Among the world’s religions, views on masturbation vary widely. Some religions view it as a spiritually detrimental practice, some see it as not spiritually detrimental and others take a situational view. Among these latter religions, some view masturbation as allowable if used as a means towards sexual self-control, or as part of healthy self-exploration, but disallow it if it is done with wrong motives or as an addiction.
Masturbation (istimna [استمناء] in Arabic) is forbidden in the hadith. It is frowned upon by some scholars, who maintain that it should only be done if one fears of committing illicit sex (fornication or adultery); even then alternatives such as getting married or fasting are preferred, as suggested in hadith.
Scholars of Islam do not generally approve of masturbation, except in extenuating circumstances. There is a statement in Quran “And those who guard their chastity, Except from their wives for then, they are free from blame; But whoever seeks beyond that, then those are transgressors.” (23:5-7)
In Islam, sexual engagement outside of marriage is a major sin, which causes the doer to be punished in this life and in the afterlife (Qiyama). Yet if one’s desire is so overwhelming one might perform a greater wrong by having sex outside marriage, masturbation becomes permissible as a necessity but in that case it will be like eating the flesh of a pig to survive from major hunger or starvation when no other food is available. So Quran says “And those who are not married should try to live in chastity, until God enriches them with His Grace” (Qur’an, 24:33)
Masturbation, like any form of sex in which seminal or vaginal fluids have been released, breaks one’s fast if performed during the daylight hours and requires a major ablution if any seminal or vaginal fluids were released.
According to Sheikh Hamed Al-Ali: “Masturbation during the daytime of Ramadan breaks the fast, based on the Hadith that a fasting Muslim gives up eating, drinking, and sexual desire for the sake of Allah. Since masturbation is a kind of sexual desire, a fasting Muslim must avoid it. Therefore, masturbation invalidates the fast as does food and as it is one of the sins that if someone does it he or she would be violating the sanctity of this month.”
Masturbation is forbidden altogether in the Shi’a sect. The Qur’an says, “The believers are… those who protect their sexual organs except from their spouses… Therefore, whosoever seeks more beyond that [in sexual gratification], then they are the transgressors.” (23:5-6) 
The four Sunni schools of jurisprudence (known as Madhaahib – the Hanafi, Shafi’i, Maliki and Hanbali schools of Fiqh) have differing stances on the issue. Some see it forbidden in certain cases (i.e. if it leads a man/woman to ignore their spouse sexually) but recommended it when they see it as a lesser evil to illicit sex. It is generally prohibited according to the Hanafi and Hanbali Mazhabs, unless one fears adultery or fornication, or is under the desire pressure, in which case, it is permissible to seek a relief through masturbation. It is prohibited all the time according to the Maliki and Shafi`i Mazhabs.