Reference: Fataawa al-Lajnah ad-Daa.imah lil-Buhooth al-‘Ilmiyyah wal-Iftaa. – Fatwa No.13476
Fataawa Ramadhaan – Volume 2, Page 448, Fatwa No.375
Question: I inform you that in the month of Ramadhaan in the year 1410 AH (1989 AD), I had an appointment at the hospital. The doctor treating me asked me to produce some semen and told me that this was imperative [and that I have to do this] at which I felt inconvenienced and coerced such that I gave him some semen for the sake of the test. This was during the day in Ramadhaan. I produced the semen by masturbating, knowing that I had no other opportunity to return to the hospital and that particular day was one that the hospital had given me the appointment for. Also, this was for an analysis for my wife and me.
O Noble Shaykh, I require you to give me a fatwa for this, and is there any expiation other than making up the [nullified] fast [of that day] because I had sound knowledge of what I was doing?
Response: If the situation is as you have mentioned, then it is obligatory that you make up the fast for the day you masturbated and there is no expiation upon you to make.
And with Allaah lies all the success, and may Allaah send prayers and salutations upon our Prophet Muhammad ﷺ and his family and his companions.