Short answer
In the Hanafi school masturbation is not permitted, as a habit or for pleasure. Some Hanafi scholars excuse a single person who does it only to escape a real fear of zina, hoping there is no sin. With your own spouse's hand it is permitted. It breaks the fast and requires ghusl if semen comes out. The Shafi'i and Maliki schools forbid it in all cases.
Masturbation (istimna') is not permissible, since the Quran limits sexual release to one's spouse. If an unmarried person is overwhelmed by desire and does it only to avoid zina, the Hanafi books say it is hoped there is no sin. Stimulation by one's own spouse is permitted.
Other madhhabs
- Shafi'i
- Haram in all cases.
- Maliki
- Haram in all cases.
- Hanbali
- Haram, but permitted for someone who fears zina and cannot marry.
Explanation
Allah praises believers "who guard their private parts, except from their wives", and says that whoever seeks beyond that are the transgressors (23:5 to 7). Most scholars take this as the proof that sexual release outside marriage, including by one's own hand, is not allowed. The Prophet ﷺ told young people who could not marry to fast, and pointed to no other outlet.
- As a habit, or to arouse oneself: sinful in the Hanafi school.
- When overwhelmed, to avoid zina: the Hanafi books say it is hoped there is no sin for an unmarried person, as the lesser of two harms. This is a concession, not a licence, and the person should still work on fasting and lowering the gaze.
- By one's spouse: permitted.
- With pornography: watching it is a separate sin and haram in every case.
Other rulings. If semen comes out with desire, ghusl is required before prayer. See when ghusl is required. Doing it while fasting breaks the fast, and the day must be made up, without kaffarah. See what breaks your fast.
If you are struggling, do not despair. Repent each time, remove triggers from your phone and room, avoid being idle and alone late at night, fast, exercise, and work towards marriage. Allah forgives the one who keeps returning to Him. See sexual thoughts and repeating a sin.
The evidence
Quran, Surah Al-Muminoon 23:5-7
وَٱلَّذِينَ هُمْ لِفُرُوجِهِمْ حَٰفِظُونَ ﴿٥﴾ إِلَّا عَلَىٰٓ أَزْوَٰجِهِمْ أَوْ مَا مَلَكَتْ أَيْمَٰنُهُمْ فَإِنَّهُمْ غَيْرُ مَلُومِينَ ﴿٦﴾ فَمَنِ ٱبْتَغَىٰ وَرَآءَ ذَٰلِكَ فَأُو۟لَٰٓئِكَ هُمُ ٱلْعَادُونَ ﴿٧﴾
And they who guard their private parts Except from their wives or those their right hands possess, for indeed, they will not be blamed - But whoever seeks beyond that, then those are the transgressors -
Sexual release is limited to one's spouse.
Quran, Surah An-Noor 24:33
وَلْيَسْتَعْفِفِ ٱلَّذِينَ لَا يَجِدُونَ نِكَاحًا حَتَّىٰ يُغْنِيَهُمُ ٱللَّهُ مِن فَضْلِهِۦ ۗ وَٱلَّذِينَ يَبْتَغُونَ ٱلْكِتَٰبَ مِمَّا مَلَكَتْ أَيْمَٰنُكُمْ فَكَاتِبُوهُمْ إِنْ عَلِمْتُمْ فِيهِمْ خَيْرًۭا ۖ وَءَاتُوهُم مِّن مَّالِ ٱللَّهِ ٱلَّذِىٓ ءَاتَىٰكُمْ ۚ وَلَا تُكْرِهُوا۟ فَتَيَٰتِكُمْ عَلَى ٱلْبِغَآءِ إِنْ أَرَدْنَ تَحَصُّنًۭا لِّتَبْتَغُوا۟ عَرَضَ ٱلْحَيَوٰةِ ٱلدُّنْيَا ۚ وَمَن يُكْرِههُّنَّ فَإِنَّ ٱللَّهَ مِنۢ بَعْدِ إِكْرَٰهِهِنَّ غَفُورٌۭ رَّحِيمٌۭ ﴿٣٣﴾
But let them who find not [the means for] marriage abstain [from sexual relations] until Allah enriches them from His bounty. And those who seek a contract [for eventual emancipation] from among whom your right hands possess - then make a contract with them if you know there is within them goodness and give them from the wealth of Allah which He has given you. And do not compel your slave girls to prostitution, if they desire chastity, to seek [thereby] the temporary interests of worldly life. And if someone should compel them, then indeed, Allah is [to them], after their compulsion, Forgiving and Merciful.
Those who cannot marry are told to stay chaste.
Sahih al-Bukhari 5066
Sahih... ومن لم يستطع فعليه بالصوم فإنه له وجاء ...
"Whoever is not able, let him fast, for it is a shield for him."
The prophetic remedy for desire before marriage is fasting.
Sahih al-Bukhari 6474
Sahih... من يضمن لي ما بين لحييه وما بين رجليه أضمن له الجنة ...
"Whoever guarantees me what is between his jaws and what is between his legs, I guarantee him Paradise."
Guarding one's private parts is rewarded with Paradise.
Where the ruling is recorded
- al-Durr al-Mukhtar with Radd al-Muhtar, Kitab al-Sawm, Bab ma Yufsid al-Sawm. Masturbation breaks the fast; it is not permitted, and for one who fears zina it is hoped there is no sin.
- al-Mughni (Ibn Qudama), Kitab al-Hudud, on istimna'. The Hanbali allowance for one who fears zina.
Checked by our team. For a more accurate answer to your own situation, consult a scholar. Quran translation by Sahih International. Hadith Arabic from the standard editions of the six books and the Muwatta, with gradings as recorded there; the English is our own rendering. Spotted a mistake? Tell us.