Masturbation is called استمناء in fiqh.
There are no Quranic verses or authentic hadith that explicitly mention it (there are weak ahadith which say that it is forbidden). However scholars have reasoned that it is included in the meaning of the verse:
والذين هم لفروجهم حافظون إلا على أزواجهم أو ما ملكت أيمانهم فإنهم غير ملومين فمن ابتغى وراء ذلك فأولئك هم العادون
And who guard their private parts. Except from their wives or from those (bondwomen who are) owned by their hands, as they are not to be blamed. However, those who seek (sexual pleasure) beyond that are the transgressors.
— Quran 23:5-7
Since obtaining pleasure by masturbation is obtaining it by a means other than one's spouse or concubine, hence it is transgression according to the verse and hence is haram.
Similarly they have relied on the hadith:
من استطاع الباءة فليتزوج، فإنه أغض للبصر، وأحصن للفرج، ومن لم يستطع فعليه بالصوم، فإنه له وجاء
He who can afford to marry should marry, because it will help him refrain from looking at other women, and save his private parts from committing illegal sexual relation; and he who cannot afford to marry is advised to fast, as fasting will diminish his sexual power.
— Bukhari
As here fasting is mentioned as the way for one to remain chaste, if masturbation was permissible it would have been mentioned instead.
The jurists differ on the ruling on masturbation:
The majority consider masturbation without a valid need, or merely to obtain sexual pleasure, to be haram.
Some of them consider it permissible or sinless if there is a need such as when one is overcome with desire and fears that he might fall into Zina and hopes that masturbation will reduce his desire and so protect him. Others consider it haram even when there is fear of Zina.
Some of them consider it permissible or even obligatory when it is certain that a person will fall into Zina and masturbation is the only way to avoid it.
Some consider it to be haram in all cases. However they note that it is a lesser sin than Zina and so a person should choose this over Zina when there is no other way, so that the sin he earns will be lower.
A minority considers masturbation to be makruh (tanzihi).
Ref:
See Mawsoo’ah al-Fiqhiyyah 4/98 and 45/272-273
From Tafsir Al Qurtubi:
سألت مالكا عن الرجل يجلد عميرة ، فتلا هذه الآية والذين هم لفروجهم حافظون - إلى قوله - العادون ... وأحمد بن حنبل على ورعه يجوزه ، ويحتج بأنه إخراج فضلة من البدن فجاز عند الحاجة ؛ أصله الفصد والحجامة
Malik was asked about a man who masturbated and he recited this verse (Quran 23:5-6)
...
Ahmad ibn Hanbal allowed it, arguing that it was the release of a bodily excess and that it was permitted in times of necessity similar to bloodletting and cupping.
From Tafsir Ibn Kathir:
وقد استدل الإمام الشافعي رحمه الله ومن وافقه على تحريم الاستمناء باليد
بهذه الآية الكريمة { وَٱلَّذِينَ هُمْ لِفُرُوجِهِمْ حَـٰفِظُونَ إِلاَّ
عَلَىٰ أَزْوَٰجِهِمْ أَوْ مَا مَلَكَتْ أَيْمَـٰنُهُمْ فَإِنَّهُمْ
غَيْرُ مَلُومِينَ } قال فهذا الصنيع خارج عن هذين القسمين، وقد قال الله
تعالى { فَمَنِ ٱبْتَغَىٰ وَرَآءَ ذٰلِكَ فَأُوْلَـٰئِكَ هُمُ
ٱلْعَادُونَ } و
Imam Shafi and his followers take this verse as evidence that
masturbation is forbidden, because it is a distinct from the two
options mentioned, and the one who masturbates is one who transgresses
the limits.
From islamQA:
Shaykh al-Islam Ibn Taymiyah was asked about a man whose desire
overwhelms him and he masturbates, and he knows that this may be dealt
with by fasting but it is too difficult for him.
He replied: With regard to water (semen) that is emitted
involuntarily, there is no sin on him for that, but he has to do ghusl
(bathe) if the water came out gushing. As for making it happen by
choice, by means of masturbation, this is haraam according to most
scholars, and it is one of the two opinions narrated from Ahmad;
rather it is the stronger of the two. According to one report, it is
makrooh (disliked), but if he is compelled to do it, such as if he is
afraid of committing zina (fornication/adultery) or becoming ill if he
does not masturbate, then in this case there are two well-known
scholarly opinions. In this case, several of the earlier and later
(scholars) granted a concession allowing it, but others forbade it.
And Allah knows best.
From IslamOnline:
This view is very close to the opinion held by the Hanafites
(followers of the Hanafi school), who maintain that masturbation is
basically forbidden, but it may be permissible under the following
conditions:
if the person is unmarried,
if he or she fears that without masturbation he/she will commit Zina, and
if the masturbation here is, rather than fulfilling a sexual desire, just to release the sexual tension resulting from stimulation.