Masturbation (the secret habit) is prohibited according to the Qur’an and Sunnah, regardless of whether one is in ihraam or not.
However, the sin is greater if the act of disobedience was done in a place or at a time of special virtue, or during an act of worship.
Shaykh al-Islam Ibn Taymiyah (may Allah have mercy on him) was asked about the sin of an act of disobedience and the hadd punishment for zina (fornication or adultery): is the sin greater if it is done during the blessed days, or not? He replied: Yes, the sin during days of special virtue or in places of special virtue is greater and the punishment is increased commensurate with the virtue of the time or place.
End quote from Majmoo‘ al-Fataawa (34/180)
However the increase in severity of the punishment has to do with quality thereof, not quantity.
Masturbation is one of the things that are prohibited whilst in ihram, but it does not render the pilgrimage invalid, according to the majority of scholars, whether it is done before or after the first stage of exiting ihram, because there is no evidence to suggest that the pilgrimage is rendered invalid by that, and it is not valid to compare it with full intercourse, because there are differences between them.
It says in al-Mawsoo‘ah al-Fiqhiyyah (4/102):
Hajj is not rendered invalid by masturbation according to the Hanafis, Shaafa‘is and Hanbalis… end quote.
An-Nawawi (may Allah have mercy on him) said in al-Majmoo‘ (7/307):
Masturbation is prohibited, with no difference of opinion (in our madhhab) because it is haraam when not in ihram, so it is more appropriate that it should be haraam when one is in ihram.
If the muhrim (pilgrim in ihram) masturbates and ejaculates, does he have to offer compensation?
There are two views concerning this matter; the correct view is that which is most well-known, which is that he must offer compensation. The second view is that he does not have to offer compensation, but the more correct view is that he does have to offer compensation.
Then he (may Allah have mercy on him) said (7/417):
If we say that he must offer compensation, then it is the same as compensation for shaving the head, as we said concerning intimacy with a woman without having intercourse. But his Hajj is not rendered invalid by masturbation, and there is no difference of scholarly opinion concerning that. End quote.
And he (may Allah have mercy on him) said:
Our view is that the compensation for shaving the head is one of the following: sacrificing a sheep, fasting for three days, or giving three saa‘s of food to six poor persons, each one receiving half a saa‘.
End quote from al-Majmoo‘ (7/389). See also: ash-Sharh al-Mumti ‘ (7/167)
Based on the above, his Hajj is valid, but he must offer the same compensation as one who shaved his head because of an ailment, as explained above.
See also the answer to question no. 206010.
He does not have to offer compensation more than once, because the second prohibited action was of the same nature as the first one, so they are interconnected. This applies if the second incident of masturbation came before offering expiation for the first incident. If he had already offered compensation, then he masturbated again, then he must offer compensation again, and it cannot be included with the previous compensation. It is not permissible for him to delay offering the first expiation in order to offer expiation only once.
Ibn Qudaamah (may Allah have mercy on him) said:
If a person (in ihram) shaves his head, then shaves it again, he must offer one compensation, so long as he did not offer expiation for the first time before doing it for a second time. If he had already offered compensation for the first time, then he shaved his head again, then he must offer expiation (again) for the second time.
End quote from al-Mughni (3/260). See also: al-Majmoo‘ (7/394) and al-Mawsoo‘ah al-Fiqhiyyah (11/90)
Shaykh Ibn ‘Uthaymeen (may Allah have mercy on him) said:
… But that is on condition that he does not delay offering compensation in order to avoid having to offer it again if he repeats the act. If he deliberately delays offering compensation so that he can repeat the act and offer compensation only once, the ruling should be such, so as to prevent him doing that.
For example, if he clips his nails twice, or he puts on tailored garments twice, or shaves his head twice, or is intimate with his wife twice or more, and the act is of the same nature, in that case he has to offer compensation once, if he has not yet done so, by analogy with the fact that if a person becomes in a state of minor impurity several times, all of the same type, then doing wudoo’ once is sufficient.
End quote from ash-Sharh al-Mumti‘ (7/190)
And Allah knows best.