I read through the answers and the obvious answer wasn’t mentioned, so I’ll mention it. As written, Jesus did not actually say that while he was on the cross. He called for Eloi, and it was misheard by other bystanders. His disciples heard him proper, and brought Eloi around so he could give Jesus some refreshment.
The reason for the passage was to indicate that not all who attended Jesus’ death were his followers, there were many who were there to mock him.