The majority of scholars have concluded that masturbation is haram. The main basis for such a ruling is 23:5-7
Those who guard their sexual organs except with their spouses
or those whom their right hands posses, for they are without blame.
But those who crave something beyond that are transgressors.
According to scholars those who masturbate would be those who are craving something beyond which is permitted in wives and right hand possesssions. They would be transgressing the boundries which Allah has given His servants in this aya to release their sexual desire.
The Prophet pbuh is reported to have said: Young men, those of you who can support a wife should marry, for it lowers your gaze and preserves your chastity; but those who cannot should fast, for it is a means of cooling sexual passion <Bukhari>
Scholars have concluded that if masturbation were permitted Muhammad pbuh would have stated so. However he directed the person to fast instead of giving him an option of masturbation.