Question: Why does Jesus say, "My God, my God, why have you forsaken me?"
Response: This is a very good question. Obviously, Jesus' words from the cross are especially significant. Everything in scripture and everything Jesus said is important, but we can be sure that His final words have special prophetic significance, and this is certainly so in the case of the quote you ask about.
When Jesus says "My God, My God, why hast Thou forsaken Me?", He is, as you no doubt already know, quoting from Psalm 22 (verse 1 in the English versions). This is important to understand, because Psalm 22 is a prophecy of Jesus' crucifixion. The rejection of the Messiah by the people (v.6), the insults they hurled at Him on the cross (vv.7-8 – compare with Matt.27:38-43), the pain of the crucifixion (vv.14-15), the piercing of His hands and feet (v.16), the dividing up of His clothing by lot (v.18) are just some of the more obvious parallels this Psalm prophesies. Jesus knew exactly what was going to happen to Him because He understood this Psalm and how it applied to His death on our behalf (cf. Matt.20:18-19). By quoting Psalm 22, our Lord makes this clear, and makes it clear to all who would later hear these words of His that He was well aware that He would have to die on our behalf in order to save us – for this reason He came into the world (Jn.3:16-17).
Some people very wrongly take this quote you ask about to be a sign of desperation on our Lord's part in His hour of trial – but nothing could be further from the truth. These words from our Savior make it clear beyond doubt that, on the one hand, He understood why He had to suffer – this was His mission – and, on the other hand, that He was supremely confident of the Father's ultimate deliverance of Him through the resurrection of His body before it had even seen decay (Ps.16; cf. Act 2:24-31), for the second half of Psalm 22 is a hymn of victory (vv.22-31). These words, therefore, were spoken entirely for our benefit, that we might know that Jesus was delivered over through the will of God (Matt.20:28), willing accepted this mission for our sake (Matt.26:42), and had complete and unbreakable faith in the deliverance that awaited Him. These words were spoken that we might believe in Him and might emulate His faith and confidence in God's deliverance even in the most terrible of circumstances. For if God delivered over His own Son to death that we might live, how would He not then give us everything (Rom.8:32)?
It is also important to note that this quote precedes by only slight moments the exhaling of His spirit, and follows the three hours of darkness that cloaked our Lord's most intense suffering on the cross. What this means is that "why hast Thou forsaken Me?" is really better translated "why did You forsake Me?", a sense that is common in the Hebrew perfect tense in the original Psalm 22:1 quote. Therefore Jesus' use of this quote is really not even a question in the true sense. He is not asking for an answer or an explanation of what has happened – He knows very well why He was allowed to suffer on the cross. Rather these words are an explanation for us: "Do you want to know why God allowed Me to be crucified? It was to save you!" This interpretation is confirmed by Jesus final statement on the cross which comes almost immediately after the Psalm 22:1 quote and is recorded in Luke 23:46: "Father, into Thy hands I commit my spirit". This is also a quote from the Psalms, Psalm 31:5, to be precise, and is a statement not only of Jesus' impending departure from life, but a statement of victorious trust in the One in whom He trusted, the One He knew would shortly deliver His humanity through resurrection. For although Jesus does not complete the quote of this verse, its remaining words would have been well known to any and all who had studied the Psalms, "You have redeemed Me, O Lord God of truth." Jesus was supremely confident of the deliverance of His body from death, and passes that confidence on to us in all of these final words from the cross. "Why hast Thou forsaken me" looks back to Jesus' successful endurance of the cross, His victory over sin for us, while "into Thy hands [... Thou has redeemed Me]", looks forward to His impending victory over death for us through His resurrection, just as Paul explains it:
He was handed over on account of our transgressions (i.e., to redeem us from sin), and was raised up on account of our justification (i.e., so that we too could be raised, having been justified by His death).
The body of Psalm 22 that follows verse one, relating all the particulars of our Lord's ordeal of the cross (vv.2-21), and the promise of victory that follows (vv.22-31), gives the explanation for the "why?" of the first verse. In quoting Psalm 22:1 from the cross, Jesus is giving us an explanation for the need for His death on our behalf, a reminder of the magnitude of His sacrifice, and a victory hymn, a victorious proclamation of His successful accomplishment of the mission given Him by the Father. Why was He forsaken? He was forsaken for you and me! And with these words Jesus makes it clear that the Father's sacrifice of Him and His offering Himself up in sacrifice were not accidental but part of, indeed the heart of the plan of God, the price above prices paid for our salvation.
[Future generations] will come and proclaim [God's] righteousness to people not yet born, for He has accomplished it (i.e., salvation)!
This final verse of Psalm 22 is also alluded to on the cross by our Lord, at the end of His ordeal of bearing our sins during the three hours of darkness and just before His exhaling of His spirit and just after receiving the water with vinegar when He says "It (i.e., salvation) has been accomplished" (Jn.19:30; Greek: tetelestai). Jesus knew very well that unless His own Father forsook Him and handed Him over to judgment, there could be no redemption, and all whom He had come to save would be lost. Without His being forsaken, salvation could never be accomplished. Thus these words "why have You forsaken Me", far from expressing doubt, show beyond all question the unfathomably deep love our Savior had for you and me, for He knew from the very beginning what it would take from Him to rescue us from death.
You will find all of this written up in considerably greater detail at the following link:
Christ was Forsaken for us in Dying for us (in Bible Basics 4A: Christology)
In Him who gave us life through His own death, who was forsaken that we might be adopted as sons, our precious Lord and Savior Jesus Christ.