Praise be to Allah.
With regard to emission of maniy (semen) that occurs when one is awake, if it occurs as a result of desire, then there are rulings that are connected to that: ghusl becomes obligatory, and it spoils the fast. But if it is emitted without desire, because of sickness, cold and the like, then no rulings apply as a consequence of that, according to the view of the majority of fuqaha’. Some of the fuqaha’ differentiate between the obligation to do ghusl and the spoiling of the fast, such as the Shaafa‘is. They think that ghusl is required because of this maniy that was emitted without desire, but they do not think that it spoils the fast.
Ad-Dardeer said in ash-Sharh al-Kabeer (1/523): … emission of maniy when awake with normal feelings of pleasure [spoils the fast] … but emission of maniy without pleasure or without the normal feelings of pleasure [does not spoil the fast] …
Ibn Qudaamah (may Allah have mercy on him) said: If he masturbates, he has committed a haraam action, but his fast is not spoiled unless he ejaculates. If he ejaculates, he spoils his fast, because it is akin to kissing in terms of provoking desire. But if he ejaculates without desire, such as one who emits maniy or madhiy because of sickness, he does not have to do anything, because it was emitted without desire, like urine, and because it was emitted involuntarily, and was not caused by any action on his part, so it is akin to a wet dream.
End quote from al-Mughni (2/128).
In Haashiyat Qalyoobi wa ‘Umayrah (2/74) it says: Ejaculation that occurs involuntarily does not break the fast. End quote.
If emission of maniy (semen) occurred involuntarily, and without pleasure, then it does not spoil the fast.
With regard to ghusl, the view of the majority of fuqaha’ is that it is not required if the maniy was emitted without desire.
Nevertheless, if the person does ghusl, that will be better and more prudent.
See also the answer to question no. 47693.
And Allah knows best.