Masturbation is called استمناء in fiqh. There are no Quranic verses or authentic hadith that directly mention it.
The view of the Shafi and Maliki schools of thought is that masturbation is unconditionally forbidden no matter what the circumstances:
The evidence presented by them is
Quran 23:5-7 And they who guard their private parts. Except from
their wives or those their right hands possess, for indeed, they will
not be blamed. But whoever seeks beyond that, then those are the
transgressors.
With the interpretation that a person himself is neither a "wife" nor a "slave" and hence the prohibition extends to himself.
From Tafsir Al Qurtubi:
قال محمد بن عبد الحكم: سمعت حَرْملة بن عبد العزيز قال: سألت مالكاً عن
الرجل يَجْلِد عُمَيرة، فتلا هذه الآية { وَٱلَّذِينَ هُمْ لِفُرُوجِهِمْ
حَافِظُونَ ـ إلى قوله ـ ٱلْعَادُونَ }. وهذا لأنهم يَكْنُون عن الذَّكَر
بعُمَيْرة؛ وفيه يقول الشاعر:
Muhammad bin Abdul Hakim said, I heard Harmalah bin Abdul Aziz saying
that: "When we asked Imam Malik about the person who masturbates, he
recited this ayah 23:5 ...
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.
They further argue that in Hadith the Prophet advised a group of youths to guard their chastity by observing fast, the argument being that if masturbation was permissible then he would have suggested that instead.
The view among the Hanafis and Hanbalis is that masturbation is permissible when the person fears falling into Zina (fornication), which is a major sin clearly mentioned in the Quran, and a lesser evil may be used to ward off a greater evil.
From Tafsir Al Qurtubi:
وأحمد بن حنبل على ورعه يجوّزه، ويحتج بأنه إخراج فضلة من البدن فجاز عند
الحاجة؛ أصله الفَصْد والحجامة.
Imam Ahmad bin Hanbal, despite his piety, declared it permissible. His
view was that its the expulsion of waste from the body and is
permissible to excrete it when one feels it necessary, and that it is
based on blood-letting and cupping.
From the view of the Hanbali scholar Ibn Taymiyah mentioned on 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.
Hanafi view mentioned on 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.