(Translated from Maqāmāt by Shehzad Saleem)
The sharī'ah does not allow Muslims to sexually gratify themselves from any other individual but their wives. The Almighty has forbidden it, and categorically stated in Sūrah Mu'minūn that people who sexually gratify themselves from anyone other than their wives will be guilty of exceeding the limits set by God. The slave-women of the times of the Prophet (sws) who were yet to be liberated from their masters were, however, exempted from this directive: if people wanted, they could also satisfy their sexual urge through them. The Qur'ān says:
وَالَّذِينَ هُمْ لِفُرُوجِهِمْ حَافِظُونَ إِلَّا عَلَى أَزْوَاجِهِمْ أوْ مَا مَلَكَتْ أَيْمَانُهُمْ فَإِنَّهُمْ غَيْرُ مَلُومِينَ فَمَنِ ابْتَغَى وَرَاء ذَلِكَ فَأُوْلَئِكَ هُمُ الْعَادُونَ (23: 5-7)
And who guard their private parts except from their wives and slave-women because for these they have no blame on them. But, those who want something beyond this are indeed transgressors. (23:5-7)
Presented above is the correct interpretation of the verse. However, some of our jurists have also argued on the basis of this verse that except for wives and slave-women other means of sexual-gratification are forbidden. Thus like adultery, homosexuality and bestiality, various forms of masturbation are also forbidden in their opinion; the only lenience that can be given in this regard is that the extent of this prohibition of masturbation is less than that of adultery, homosexuality and bestiality, and if people in order to protect themselves from these sins indulge in such sexual self-gratification to vent their emotions, then it can be expected that perhaps the Almighty would not punish them.
In my opinion, this argument is very weak, and in fact incorrect as per the linguistic principles of Arabic. The reason for this is that the preposition عَلَى (on/upon) does not collocate with حَافِظُوْن (those who guard) and thus there necessarily exists a tadmin in this expression, and words such asعَنِ الوُقُوعِ عَلَى أَحَدٍ (from indulging with anyone) are suppressed after حَافِظُوْن. Thus, the object from which the exception is sought in this expression is not the ways of sexual-gratification: it is the individuals from whom a person can establish sexual relations. It is evident from this discussion that this verse does not mean that no way of sexual-gratification is allowed except through wives and slave-women; the correct meaning is that except for wives and slave-women, one cannot appease one's sexual urge with any other individual. This is the correct meaning of the verse. Thus, it can be said with full certainty that there is no injunction or general principle in the Qur'ān on the basis of which masturbation can be regarded as prohibited or undesirable. No different is the case of the Hadīth. Its whole corpus is devoid of any narrative on this subject which is acceptable to the scholars of Hadīth.
Thus, in this regard, the correct opinion is the one which is presented by Imām Ibn Hazm in his al-Muhallā with all the requisite arguments; he has mentioned through a chain of narration that authorities like Hasan al-Basrī, 'Amr ibn Dīnār, Ziyād Abū al-A'lā and Mujāhid regard masturbation as allowable. These authorities mostly narrate such things from the Companions of the Prophet (sws).