I do not understand the context of Psalm 22. Who was David talking to? Was he describing a vision? How can this Psalm be considered Messianic?
To answer your question, in Psalm 22 David is seeing a vision of the sufferings of the Messiah and writes up the sufferings and the glory that followed. He was singing Hebrew poetry as all of the Book of Psalms is. He does not describe anything that David himself suffered, but he is describing the sufferings of the Messiah based upon a vision he saw.
The following quotation from our book Ha-Mashiach (which is a study of Old Testament prophecy concerning the First Coming of the Messiah) will provide you with some more details:
Psalm 22 is the most famous of the Messianic Psalms, the entire psalm being devoted to the events of the First Coming and a few aspects of the Second. The psalm divides into two main parts, the first dealing with the suffering of Messiah, followed by His exaltation in the second. The whole psalm could be viewed as a poetic version of Isaiah 53, although the psalm was in fact written before the prophecy of Isaiah.
The Sufferings of the Messiah — 22:1-21
Messiah’s Cry for Help — 22:1-2
These verses find Messiah crying out in deepest anguish. It is no accident that these are the very words that Jesus cried out while hanging on the cross. He quoted these words after a period of three hours of intense darkness. During those three hours the entire wrath of God, due to the sins of Israel and the world, was poured out upon Him. This is the one and only place in the Gospel accounts that Jesus addresses God as “my God.” On every other occasion, and there are over 170 references, Jesus says “Father” or “my Father.” It is made very clear that Jesus enjoyed a very special, unique relationship with God. On the cross, however, Jesus was dying for the sins of the world, and was experiencing a judicial relationship with God, not a paternal one; hence His cry of “my God, my God” instead of “my Father, my Father.”
God’s Past Deliverance — 22:3-5
These verses recount the past deliverances of God. God is fully able to deliver, yet is choosing not to.
Messiah Despised — 22:6-8
These verses describe, in terms similar to Isaiah 53, the taunts and jibes of evil men at the suffering of Messiah. The words used here are indeed very similar to the words of ridicule used by the crowds at the crucifixion of Jesus. He is reproached, scorned, and taunted.
God is Messiah’s Trust — 22:9-11
These verses state that Messiah has trusted in God from His birth. There are references here to the mother of Messiah but, as in all other messianic prophecies, there is never any mention of a human father. Messiah would be born of a virgin as prophesied in Isaiah 7:14.
Description of the Agony — 22:12-18
These verses describe the suffering of Messiah, and some of these words are almost quoted in the New Testament.
1. Surrounded and stared at — 22:12-13.
2. Physical agony — 22:14-17.
i. I am poured out like water. This emphasizes excessive sweat.
ii. All my bones are out of joint. After the nailing on the ground, the cross would be raised to the vertical and dropped into a deep slot in the ground. The shock of this action would cause multiple dislocations.
iii. My heart is like melted wax. A Hebrew phrase meaning “a ruptured heart,” evidenced by the pouring out of blood and water.
iv. My strength is dried up like a potsherd. His strength is totally gone.
v. My tongue cleaves to my jaws. His tongue cleaves to the roof of His mouth, emphasizing excessive thirst. After six hours on the cross, three of them in total darkness, Jesus said “I thirst.” This meant more than physical thirst. During those three hours of intense darkness Jesus suffered the outpouring of God’s wrath; the pangs of Hell itself. Jesus had previously spoken of a rich man who, after only a few moments in Hell had said “I thirst” (Luke 16). Jesus’ saying of these same words reflects the extreme suffering of the pain of Hell which He experienced while hanging on the cross.
vi. They pierced my hands and my feet. The Hebrew word for piercing used here is not the same as that used in Zechariah 12:10. The word used in Zechariah means “to thrust through” and would be consistent with the Roman spear which pierced Jesus’ side. The word used here in Psalm 22 is the word which would be used, for example, of ear piercing and would be consistent with the nailing of Jesus’ hands and feet to the cross.
vii. I can count all my bones. His bones are protruding.
3. They divide my garments among them — 22:18. In verse 18, Messiah’s clothes are divided amongst His tormentors by the casting of lots. Once again, this was quite literally fulfilled at Jesus’ crucifixion (Matthew 27:35, Mark 15:24, Luke 23:34, John 19:23-24).
All of the above must refer to Messiah since clearly none of these things ever happened to David. Messiah’s Prayer for Help — 22:19-21
Verses 19-21 are again a cry for help from Messiah while still hanging on the cross.
The Exaltation of the Messiah — 22:22-31
With His suffering complete, verses 22-31 turn and speak of Messiah’s exaltation. In verse 22 Messiah will praise God in the midst of the assembly. But how, if He died in verses 1-21 is this possible? Clearly this can only be possible by resurrection. The rest of the psalm goes on to describe what happens after His resurrection, culminating in His Second Coming and the establishment of His kingdom.
A Note on Verse 16
Some wish to translate the verse as “like a lion, my hands and my feet,” instead of “they pierced my hands and my feet.” The former is based on the pointing of the Masoretic text and the latter on the Septuagint, a Greek translation of the Hebrew text that preceded the Masoretic text by over one thousand years, and hence closer to the original writing. While it is true that the writer uses several animal motifs in the context, the Psalmist only uses animalistic terms to describe his enemies and not himself. Hence both the context and the antiquity of the Hebrew text behind the Septuagint favor the rendering of “pierce.”
Psalm 22 teaches that:
In extreme agony, Messiah would cry out for God’s help.
Messiah would be a despised and rejected individual.
In the agony of death Messiah would be stared at and mocked.
The Messiah’s bones would all be pulled out of joint.
The Messiah’s heart would rupture.
The Messiah would suffer an extreme degree of thirst.
Messiah’s hands and feet would be pierced.
Messiah’s clothing would be divided by the casting of lots.
At the point of death, Messiah’s trust would be in God the Father.
Messiah would be resurrected.