Your Hajj is valid according to the more correct of the two scholarly views, but you have to repent to Allaah from that, because masturbation is haraam during Hajj and at other times, as Allaah says (interpretation of the meaning):
“And those who guard their chastity (i.e. private parts, from illegal sexual acts)
Except from their wives or (the slaves) that their right hands possess, for then, they are free from blame;
But whoever seeks beyond that, then those are the transgressors”
And because it causes a great deal of harm as explained by the scholars.
We ask Allaah to guide us and you and to grant us strength. You also have to offer a sacrifice to be slaughtered in Makkah and its meat distributed to the poor.
Fataawa al-Shaykh Ibn Baaz, 17/139.
For the ruling on masturbation please see question no. 329.