The strongest opinion is that masturbation is haram due to the evidence from the Qur’aan,
(إِلَّا عَلَىٰٓ أَزۡوَٰجِهِمۡ أَوۡ مَا مَلَكَتۡ أَيۡمَٰنُهُمۡ فَإِنَّهُمۡ غَيۡرُ مَلُومِينَ ‘وَٱلَّذِينَ هُمۡ لِفُرُوجِهِمۡ حَٰفِظُونَ)
“... and they who guard their private parts, except from their wives or those their right hands possess, for indeed, they will not be blamed.’ (Al-Mu’minoon 5-6)
This aayah proves that sexual desire can only be fulfilled with one’s wife or female slave. Furthermore, the Prophet ﷺ said,
“O young men, those from you who are able to marry, they should marry, because it helps the most to lower the gaze and remain chaste. Those who are unable to do so, let them fast, because that will reduce sexual urge.” (Bukhaary, 5066)
Hence, the Prophet ﷺ instructed the person unable to marry to fast. If masturbation was allowed, the Prophet ﷺ would have instructed people to do that. Therefore, masturbation is prohibited, although there are some scholars who maintain that it is disliked. In any case, the strongest opinion is that it is unlawful, and it is like any other sin. Thus, if a person repents to Allaah, then Allaah will forgive him. If he is remorseful of what he did and is firm that he will not repeat it, Allaah will forgive him. If fornication, which is a greater sin in terms of its impact and shame, and likewise ascribing partners to Allaah, murder and the like, are some things Allaah will forgive for the one who turns to Him and Allaah will exchange the bad deeds into good deeds. Then, when you repent to Allaah being firm that you will not repeat sins, Allaah will forgive you and change your bad deeds to good ones. The same applies to ascribing partners to Allaah, murder or fornication. Allaah says,
)وَٱلَّذِينَ لَا يَدۡعُونَ مَعَ ٱللَّهِ إِلَٰهًا ءَاخَرَ وَلَا يَقۡتُلُونَ ٱلنَّفۡسَ ٱلَّتِي حَرَّمَ ٱللَّهُ إِلَّا بِٱلۡحَقِّ وَلَا يَزۡنُونَۚ وَمَن يَفۡعَلۡ ذَٰلِكَ يَلۡقَ أَثَامٗا يُضَٰعَفۡ لَهُ ٱلۡعَذَابُ يَوۡمَ ٱلۡقِيَٰمَةِ وَيَخۡلُدۡ فِيهِۦ مُهَانًا إِلَّا مَن تَابَ وَءَامَنَ وَعَمِلَ عَمَلٗا صَٰلِحٗا فَأُوْلَٰٓئِكَ يُبَدِّلُ ٱللَّهُ سَيِّـَٔاتِهِمۡ حَسَنَٰتٖۗ وَكَانَ ٱللَّهُ غَفُورٗا رَّحِيمٗا(
“… and those who do not invoke with Allaah another deity or kill the soul which Allaah has forbidden to be killed, except by right, and do not commit unlawful sexual intercourse; and whoever should do that will meet a penalty. Multiplied for him is the Punishment on the Day of Resurrection, and he will abide therein humiliated, except for those who repent, believe and do righteous work. For them Allaah will replace their evil deeds with good, and ever is Allaah Forgiving and Merciful. (Al-Furqaan 68-70)
These sins are far greater than that of masturbation.