Islam forbids all forms of sexual relationships except in legal marriage or with who the right hand possesses. What the right hand possessed was in the past and no longer exists in these days. Therefore, marriage is the only lawful way to relief one’s desires. If a man fears for himself that he may fall into fornication, masturbation is hereby considered as committing the least of two evils, and he would not be punished for that because he freed himself from falling into zina.
Aside that, if he can avoid fornication by fasting a lot, worship, and recite the Quran, he is not allowed to masturbate.
We emphasize here that the permission to masturbate is within the limits of necessity, and for the fear of committing fornication.
However this conditional permission does not legalize what is forbidden, such as masturbation or any other related acts.