A Ruling Concerning Masturbation
The pressing need to relieve himself of sexual tension may drive a young man to masturbation.
The majority of scholars consider it haram. Imam Malik bases his judgement on the verse, Those who guard their sexual organs except with their spouses or those whom their right hands possess, for (with regard to them) they are without blame. But those who crave something beyond that are transgressors, (23:5-7) arguing that the masturbator is one of those who "crave something beyond that."
On the other hand, it is reported that Imam Ahmad Ibn Hanbal regarded semen as an excretion of the body like other excrete and permitted its expulsion as blood letting is permitted. Ibn Hazm holds the same view. However, the Hanbali jurists permit masturbation only under two conditions: first, the fear of committing fornication or adultery, and second, not having the means to marry.
We are inclined to accept the opinion of Imam Ahmad in a situation in which there is sexual excitation and danger of committing the haram. For example, a young man has gone abroad to study or work, thereby encountering many temptations which he fears he will be unable to resist, may resort to this method of relieving sexual tension provided he does not do it excessively or make it into a habit.
Yet better than this is the Prophet's advice to the Muslim youth who is unable to marry, namely, that he seek help through frequent fasting, for fasting nurtures will-power, teaches control of desires, and strengthens the fear of Allah. The Prophet (peace be on him) said, Young men, those of you who can support a wife should marry, for it keeps you from looking at women (lit., lowers your gaze) and preserves your chastity; but those who cannot should fast, for it is a means of cooling sexual passion. (Reported by al-Bukhari.)