masturbation is generally considered prohibited because it is not the normal way of fulfilling sexual desire; however it is a deviation – and that is enough to condemn it, even though this act does not fall under the category of absolute prohibition like Zina. However, the law of necessity, which is one of the principles of Shari`ah, should also apply here. For example, if someone is afraid that he would commit a greater sin like Zina or he will be harmed by some psychological disorders, then the ban on masturbation would be relaxed just to remove the hardship, based on the Shari`ah principle that states that “necessity is judged according to the circumstances that warrant it.”
However, after masturbating one is required to perform Ghusl after masturbation because the Prophet (peace and blessings be upon him) said: “What requires water (ghusl) is if you see water (i.e., semen) coming out of you.” (Reported by Muslim).