Acts 2
25 For David says concerning Him:

‘I foresaw the Lord always before my face,
For He is at my right hand, that I may not be shaken.
26 Therefore my heart rejoiced, and my tongue was glad;
Moreover my flesh also will rest in hope.
27 For You will not leave my soul in Hades,
Nor will You allow Your Holy One to see corruption.
28 You have made known to me the ways of life;
You will make me full of joy in Your presence.’

From my commentary.
Peter recognized that though this Psalm spoke of David, it spoke of someone greater than David – the Messiah, Jesus the Christ. Jesus may have taught Peter this when He instructed the disciples in the Scriptures.

I will add that the Holy Spirit helped Peter recall this verse. If you study the Word and have it in your soul, the Spirit can help you recall the truth inside at specific times to encourage and to build up.