Praise be to Allah.
Masturbation (the “secret habit”) is haraam, based on evidence from the Qur’an and Sunnah, and it is not permissible for men or women to do it. Young men and women should seek help through fasting and supplication, and whoever is able to get married should get married, for marriage offers the strongest protection. The one who is not able to get married should fast, because it is a shield and protection against the pressures of desire. Fasting reduces the impacts of the Shaytaan and weakens physical desire.
It says in ad-Durr al-Mukhtaar wa Haashiyat Ibn ‘Aabideen (4/27): Masturbation is haraam, and subject to a disciplinary punishment (ta‘zeer). End quote.
Ash-Shaafa‘i (may Allah have mercy on him) said in al-Umm (5/102): Masturbation is not permissible. End quote.
An-Nawawi (may Allah have mercy on him) said in al-Majmoo‘ (16/421):
Masturbation, which means causing the emission of semen by hand, is haraam; this is the view of most scholars. End quote.
Al-Mirdaawi mentioned something similar in al-Insaaf (10/252).
Ibn Duwayyaan said in Manaar as-Sabeel (2/383): It is haraam to masturbate for both men and women, because Allah, may He be exalted, says (interpretation of the meaning): “And they who guard their private parts” [al-Mu’minoon 23:5]. Then he quoted other evidence.
See also the answer to question no. 329.
Masturbation invalidates the fast if it results in the emission of maniy (semen). But if no ejaculation occurs, it does not invalidate the fast.
Ibn ‘Aabideen said in Radd al-Muhtaar (2/399): The same applies with regard to masturbation with the hand, i.e., that it does not invalidate the fast, but this applies if no ejaculation occurs. However, if ejaculation occurs, he must make up that day’s fast. End quote.
In al-Mawsoo‘ah al-Fiqhiyyah al-Kuwaitiyyah (4/100) it says:
Masturbation with the hand invalidates the fast according to the Maalikis, Shaafa‘is and Hanbalis, and most of the Hanafis are of the same view. End quote.
See also the answer to question no. 38074.
If a man masturbates and feels the maniy (semen) moving, so he holds his penis and nothing comes out of it, then there are two scenarios:
The first scenario is that it is retained and nothing comes out, even after a while. In this case, he does not have to do ghusl according to the majority of scholars, and his fast is not invalidated.
The second scenario is that it comes out when he lets go of his penis, or a short time after that. This emission makes ghusl obligatory and invalidates the fast.
Ibn Qudaamah (may Allah be pleased with him) said:
If he feels the maniy moving with desire, and he holds onto his penis, so nothing comes out of it, he does not have to do ghusl according to the apparent meaning of the words of al-Khuraqi, one of the two views narrated from Ahmad, and the view of most of the fuqaha’…
Then he said, supporting this view and refuting the other view:
We have [in support of this view] the fact that the Prophet (blessings and peace of Allah be upon him) connected doing ghusl to seeing and gushing by saying, “If she sees water” and “if the water gushes, then do ghusl.” So the ruling does not apply if that is not the case.
Then he said:
Based on that, if the maniy comes out after that, he must do ghusl, regardless of whether he did ghusl before it came out or not, because it is maniy that was emitted because of desire, therefore it made ghusl obligatory, just as if it had come out at the time when it first moved.
Ahmad (may Allah have mercy on him) said, concerning a man who had intercourse but did not ejaculate, then he did ghusl, then the maniy came out of him: he must do ghusl. He was asked about a man who dreamt that he was having intercourse, then he woke up and did not see anything, but when he got up and walked, maniy came out of him. He said: Let him do ghusl.
End quote from al-Mughni (1/128-129).
To sum up:
If maniy was not emitted whilst masturbating, because the individual held onto his penis, then it came out after that, with or without urine, then he has spoiled his fast and must make it up.
And Allah knows best.