Praise be to Allah.
It is haraam to masturbate in Ramadan and at other times, but during the day in Ramadan the prohibition is more emphatic, because it is combining engaging in masturbation, which is haraam, with spoiling the fast and transgressing the sanctity of this obligatory duty.
What the one who falls into that must do is repent to Allah, may He be exalted, and beware of His punishment.
But if the maniy did not come out, or the individual suppressed it and nothing came out of him, not even a drop, at that moment or afterwards, then his fast is still valid.
It says in Kashshaaf al-Qinaa‘ (2/318), discussing things that spoil the fast: … Or he masturbates and ejaculates, because if the fast is spoiled by kissing that leads to ejaculation, then it is more likely that it is spoiled by this, and even if he does not ejaculate, he has still committed a haraam action, although he has not spoiled his fast. End quote.
If someone suppresses the maniy and it is not emitted, then something comes out with the urine that may be maniy or madhiy, then he may distinguish between them based on the characteristics of each, as described by the scholars.
What appears to be the case is that what came out of you is maniy [semen], because it is what is usually emitted due to masturbation, and because it is the one that may be described as “matter”. Maniy is thick, unlike madhiy, which is thin and looks more like water.
Ibn Qudaamah (may Allah have mercy on him) said:
Section: If someone senses the movement of maniy when feeling desire and takes hold of his penis so that it will not be emitted, he does not have to do ghusl according to the apparent meaning of the words of al-Khuraqi, one of the two reports narrated from Ahmad, and the view of most of the fuqaha’.
The well-known view that was narrated from Ahmad is that he does have to do ghusl; he does not think that the water [semen] could go back, and his view is that the individual should do ghusl.
Al-Qaadi did not mention any difference of opinion concerning the obligation to do ghusl. He said: Because janaabah occurs when semen moves from its original location, and that happened, so janaabah is present and ghusl must be done as a result.
And because ghusl is connected to physical desires, and that occurred when it [the semen] moved, so that is similar to if it were emitted.
We may say that the Prophet (blessings and peace of Allah be upon him) connected ghusl to seeing it and to its gushing by saying: “If you see water [semen],” and “If the water [semen] gushes, then do ghusl.” So the ruling of becoming junub does not come into effect without emission of semen.
What he mentioned on the basis of the root meaning of the word janaabah is not valid, because the root meaning may refer to someone who is junub as a result of semen having departed from his body, and that does not happen except when it is emitted from his body, or it may refer to his avoiding or staying away (mujaanabah) from the prayer or the mosque, or other things that are not permitted when one is junub.
If he is deemed to be junub when maniy is emitted, this description does not apply if maniy is not emitted, because when a description is derived from something, it is not necessarily applicable in all cases. The fact that desire should be present in order to deem the individual to be junub does not mean that the mere presence of desire should lead to that ruling, because it is one of the prerequisites for the ruling and is to be taken into consideration, but it cannot be the sole reason for the ruling.
Moreover, touching women invalidates wudoo’ if it is accompanied by desire, but desire on its own does not the ruling applicable in either case, although this matter (desire) is still to be taken into consideration in order for the ruling to become applicable.
What could be concluded from the words of Ahmad here is that if the semen moves (from its original place), it will inevitably be emitted, but that may be delayed; therefore ghusl should be delayed until it is emitted from the body.
Based on that, if maniy is emitted after that, the individual must do ghusl, regardless of whether or not he already did ghusl before that, because it is maniy that was emitted as a result of desire, so ghusl is obligatory, just as is the case if it is emitted when it first moves from its original location.
Ahmad (may Allah have mercy on him) said, concerning a man who had intercourse but did not ejaculate, then did ghusl, then emitted maniy: He has to do ghusl (again).
He was asked about a man who dreamt that he was having intercourse, then he woke up and did not see anything, then when he walked, he emitted maniy. He said: He had to do ghusl.
Ahmad stated that ghusl is obligatory for one who has intercourse, then sees water [semen] after doing ghusl; this is a similar case.
We pointed out that the one who senses the maniy moving [from its original location], but it is not emitted, does not have to do ghusl. This implies that ghusl is obligatory when it is emitted, lest that lead anyone to conclude that it is not obligatory at all, when maniy moves because of desire and when it is emitted.
End quote from al-Mughni (1/128-129).
Based on that:
If it is clear that what came out of you was maniy [semen], then it spoiled your fast, and you must, in addition to repenting to Allah, may He be exalted, make it up.
Shaykh Ibn ‘Uthaymeen (may Allah have mercy on him) said:
The words “if he masturbated” mean: if he caused maniy [semen] to be emitted by any means, whether with his hand, or by rubbing against the ground, and the like, until he ejaculated. In that case, his fast was spoiled by that. This is the view of the four imams (may Allah have mercy on them), Maalik, ash-Shaafa‘i, Abu Haneefah and Ahmad…
End quote from ash-Sharh al-Mumti‘ (6/373).
And Allah knows best.