Matthew 27 records this. Highly improbable Matthew would make up this saying and put it into Jesus' mouth since it makes it look like Jesus doesn't trust the Father, and it makes it look like Jesus forgot His mission.
Why did Jesus say this? He quoted the first verse of Psalm 22 verbatim. This is a messianic Psalm according to most biblical scholars. When you cross-examine (pun intended) Psalm 22 with what happened to Jesus in the gospels' crucifixion narratives, the similarities are shocking! I'll let you compare them yourself.
3: Jesus Was Called "The King Of The Jews"
Plaque nailed above Jesus' head read "King Of The Jews". Recorded in Mark