In his book, The Lawful and the Prohibited in Islam, Sheikh Yusuf Al-Qaradawi states:
“The majority of Muslim scholars consider masturbationharam (unlawful). Imam Malik bases his judgment 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.’ (Al-Mu’minoon:5-7)
Imam Malik argued 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 the same way 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 [adultery]. 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 a habit. Yet, better than this is the Prophet’s advice to the Muslim youth who is unable to marry, namely, that he seeks help through frequent fasting, for fasting nurtures will-power, teaches control of desires, and strengthens the fear of Allah. The Prophet (peace and blessings be upon him) is reported to have said, ‘O youth, whoever of you is able to marry, let him marry, for it spares one looking at what one should not, or lapsing in adultery. And if he cannot marry, let him observe fasting, for it is a shield against evil.’ (Reported by Muslim.)
thus, we do advise you to have your wife with you and not to stay alone. If not, i.e., if there is no way to have her with you and there is a dire necessity to be away from her, then you should make use of the following:
1- Observing fasting, because it bolsters one’s faith, preserves chastity, and protects one from evil thoughts.
2- Observing moderation in eating and drinking in order to avoid stimulating your desire.
3- Keeping away from anything that is sexually stimulating, such as pornographic pictures, erotic films and love songs.
4- Choosing good and righteous friends.
5- Keeping yourself busy in worship and spiritual acts.
6- Interacting with activities of the society in such a way that it keeps you away from thinking about sex.
7- Avoiding gatherings and places that bring men and women physically close to each other.
8- Trying to admire natural things such as flowers and beautiful scenery, which do not stimulate one sexually, instead of admiring girls and women.
If you find the previous things useful, then it is forbidden for you to masturbate. However, if you find that you cannot relieve yourself except through masturbation, and you fear you may lapse in adultery if you do not masturbate, then the juristic rule which states that the lesser evil is to be suffered in order to fend off the major one applies to you, as masturbation is deemed to be the lesser of two evils in this case. But, we would like to stress that this may be done only in the case of dire necessity, when all solutions prove to be of no avail.