In Psalms 22:16-18 is David talking about himself? How do you know this is a messianic prophecy?
We cannot "prove" that David is talking about the Messiah in Psalm 22:16-18. However, if we analyze what is said there, if we take into account the general truth that there are forward-looking messianic prophecies in the Old Testament, and if we look at the life of both Jesus and David, the case for this being a prophecy of the Messiah is extremely strong.
First of all, let us look at the details of this passage. David describes someone (himself? The Messiah?) who has been surrounded by enemies and whose hands and feet have been pierced. We can argue that David is talking about himself, but the chances that David’s hands and feet were indeed pierced seems extremely remote. The fact is that crucifixion was not invented until the 4th century BC by the Persians, as far as we know. Even then, the Persians did not pierce hands and feet. It was the Romans who invented crucifixion by piercing of hands and feet–over eight hundred years after David died. This points strongly toward Jesus about whom it is a historical fact that he was crucified. Next, there is the part about "counting all my bones." Admittedly, this is a more obscure reference than the piercing of hands and feet, but it just so happens that none of Jesus’ bones were broken, while the legs of the two thieves on either side of him were broken under orders from the Jews who wanted the crucifixions on the eve of the Passover to be ended. If all we had in Psalm 22 was the mentioning of the bones, this would be a weak case for a messianic prophecy, but it supports the conclusion that this is about Jesus. Finally, there is the mentioning of the dividing and the gambling over the clothes of the person described by David. As with the piercing of hands and feet, this is a rather obvious reference to details we know to be true of Jesus, for which it is extremely unlikely that something even remotely like this happened to David. As John 19:23 reports (from an eye-witness to the events), they divided Jesus’ clothing among them but when they came to the more valuable undergarment, they cast lots rather than destroy the cloth made all of one piece. It seems absolutely compelling that the description of Psalm 22:16-18 applies to Jesus and not to David. The conjunction of all the information, arguably, points to Jesus of Nazareth and perhaps to no other person who has ever lived. That David might be talking about himself seems an untenable proposition.
Then there is the general fact that in the Old Testament there are many examples of predictive prophecies which point toward Jesus of Nazareth. The fact is that Psalm 22:16-18 does not stand alone. There is a prophecy that the Messiah will enter Jerusalem on a colt, the foal of a donkey, that he will be betrayed for thirty pieces of silver, that he will be pierced, that he will be silent when accused and many more, that he will be born in Bethlehem, but come from Galilee. The overall thrust of the evidence is that it is not just David who speaks about the Messiah in prophetic terms. This adds weight to what would admittedly seem the unlikely event that David would be talking in his poem about a person who would not live for one thousand years after he wrote the poem. David himself has other predictive prophecies, such as Psalm 16:8-11 and Psalm 110:1-4. This fact lends further credence to the conclusion that David is speaking of the Messiah in Psalm 22:16-18.
In conclusion, we cannot "prove" in the mathematical/logical sense that David is talking about the Messiah in Psalm 22:16-18, but the weight of the evidence leans overwhelmingly in that direction. The inference of the evidence is that David is not talking about himself in this famous passage.