All perfect praise be to Allaah, The Lord of the Worlds. I testify that there is none worthy of worship except Allaah, and that Muhammad is His slave and Messenger.
Masturbation (for both men and women) is forbidden in Islam based on the following pieces of evidence:
Evidence from the Quran:
Imaam Ash-Shaafi’i stated that masturbation is forbidden based on the following verses from the Quran; Allaah Says (what means): {And those who guard their chastity (i.e. private parts, from illegal sexual acts). Except from their wives or (the captives and slaves) that their right hands possess, - for them, they are free from blame. But whoever seeks beyond that, then those are the transgressors} [Quran 23: 5-7] Here the verses are clear in forbidding all illegal sexual acts including masturbation. Sexual acts are lawful only with the wife or with what one right hand’s possess, and whoever seeks any means beyond this, is a transgressor. Allaah also Says (what means): {And let those who find not the financial means for marriage keep themselves chaste, until Allah enriches them of His bounty.} [Quran 24:33] This verse also clearly orders whoever does not have the financial means to marry to keep himself chaste and be patient in facing temptations (including masturbation) until Allaah enriches him with His bounty.
Evidence from the Sunnah of the Prophet said:
Abdullaah Ibn Mas’ood said, "We were with the Prophet while we were young and had no wealth whatsoever. So Allaah’s Messenger said, "O, young people! Whoever can marry should marry, as it helps to lower one’s gaze, and protects one’s private parts, and whoever is not able to marry should fast, as this helps him to protect his private parts (from committing illegal sexual intercourse). " [Al-Bukhari and Muslim] The Hadeeth orders men who are not able to marry to fast despite the hardship encountered in doing so, and not to masturbate despite the ease of doing it. There are additional pieces of evidence that can be cited to support this ruling on masturbation. Allaah knows what is best and most correct.
As for breaking the habit of masturbation, we recommend the following:
1. The motive to seek a cure for this problem should be solely in following Allaah’s orders and fearing His punishment.
2. The permanent and immediate solution to this problem is to marry as soon as a person is able to do so, as clarified in the Prophet’s Hadeeth.
3. Keeping oneself busy with what benefits him in this world and the Hereafter as this is essential in breaking this habit before it becomes a second nature which would be difficult to get rid.
4. Lowering one’s gaze from looking at forbidden things such as pictures, movies etc. and this helps one suppress his desire before it leads him to committing what is forbidden. Allaah orders men and women to lower their gaze; Allaah Says (what means): {"Tell the believing men to lower their gaze (from looking at forbidden things) and protect their private parts (from illegal sexual acts, etc.). That is purer for them. Verily, Allaah is All-Aware of what they do. And tell the believing women to lower their gaze (from looking at forbidden things) and protect their private parts (from illegal sexual acts, etc.) ... } [Quran 24:30-31]Moreover, the Prophet said: ''Do not follow a casual (unintentional) look (at forbidden things) with another look. " [At-Tirmithi] This is a general instruction by the Prophet to abstain from anything that may sexually excite a person because it might lead him/her to commit what is forbidden.
5. Using one’s available leisure time in worshipping Allaah and increasing religious knowledge.
6. Being cautious not to develop any medical symptoms that may result from masturbation such as weak eyesight, weak nervous system, and/or back pain. In addition to this, feeling guilty and being anxious can lead to missing obligatory prayers because of the need of performing Ghusl (ritual bath) after every incidence of masturbation.
7. Avoiding the illusion that some youth have, that masturbation is permissible because it prevents them from committing illegal sexual acts such as fornication or even homosexuality.
8. Strengthening one’s willpower and avoiding spending time alone as recommended by the Prophet when he said "Do not spend the night alone" [Ahmad]
9. Acting according to the Prophet’s aforementioned Hadeeth and fast when possible, because fasting weakens one’s sexual desire.
10. Following the Prophet’s recommendation concerning the etiquette of getting ready for bed, such as reading well-known supplications, sleeping on the right side, and avoiding sleeping on the belly (as this was forbidden by the Prophet).
11. Striving hard to be patient and chaste, because persistence will eventually lead to attaining those qualities as a second nature, as the Prophet said: ''Whoever seeks chastity Allaah will make him chaste, and whoever seeks help from no one else but Allaah, Allaah will help him, and whoever is patient, Allaah will make it easy for him, and no one has ever been given anything better than patience." [Al-Bukhari]
12. Repenting, asking forgiveness from Allaah, doing good deeds, and not losing hope are all prerequisites to overcoming this problem. It should be noted that losing hope is one of the major sins and one of the characteristics of non-Muslims, so you should never lose hope in Allaah’s Mercy.
13. Finally, Allaah is the Most Merciful and He always responds to whoever calls upon Him. So, supplicating Allaah for forgiveness will be accepted, Allaah willing.
Allaah knows best.