Praise be to Allah.
It should be noted that this habit is haraam according to sharee’ah, as is indicated in the Book of Allaah and the Sunnah of His Prophet (peace and blessings of Allaah be upon him). We have already quoted the evidence in detail in question no 329. This habit is something that is repulsive to sound human nature and reason, and it is not befitting for a Muslim to lower himself to this level.
It should also be noted that sin has negative consequences a person, in this world and in the Hereafter, if he does not repent and if Allaah does not bestow His mercy upon him. We have already discussed this in the following questions: 23425, 8861, 45040.
With regard to the ruling on the matter mentioned in the question, if you engaged in the secret habit but no semen came out as a result, then the fast is not invalidated, according to the most sound scholarly opinion, because what matters is the emission of semen. If it did come out then the fast was invalidated and it has to be made up. If it did not come out then the fast is not invalidated. But whatever the case you have to repent to Allaah and seek His forgiveness for wasting the fast by doing such things.
Semen may be emitted a short while later even if you prevented it from coming out. In that case the fast of that day is invalidated and you have to make it up. If you do not know how many days your fast was invalidated, then try to work it out until you reach a figure you think is most likely, then make up the days that you owe.
Shaykh Ibn ‘Uthaymeen said in Sharh Zaad al-Mustaqni’: Is it possible for semen to move without being emitted?
Yes, that is possible, when a person’s desire subsides for some reason, then semen does not come out.
And they gave another example: if a person holds his penis so that no semen comes out. Even though the fuqaha’ gave this as an example, it is very harmful. The fuqaha’ (may Allaah have mercy on them) gave something as an example regardless of how harmful or not it may be. But usually in such cases the semen is released after the person lets go of his penis.
Some of the scholars said that ghusl is not required if the semen moves. This is the view favoured by Shaykh al-Islam and is the correct view. The evidence for that is as follows:
1 – The hadeeth of Umm Salamah in which it says: “Yes, if she sees water.” He did not say, If she feels it moving. If ghusl were required when it moves, then he (peace and blessings of Allaah be upon him) would have pointed it out because there would have been a need to do so.
2 – The hadeeth of Abu Sa’eed al-Khudri: “Water is for water.” In this case there is no water (i.e., emission). So this hadeeth indicates that if there is no water (emission) there is no need for water (ghusl).
3 – The basic principle is that the person remains pure (taahir) and no ghusl is required. This principle cannot be changed unless there is evidence to that effect.
Al-Sharh al-Mumti’, 1/280
See also: al-Furoo’, 1/197; al-Mabsoot, 1/67; al-Mughni, 1/128; al-Majmoo’, 2/159; al-Mawsoo’ah al-Fiqhiyyah al-Kuwaitiyyah, 4/99
And Allaah knows best.