Answer
(Fatwa: 368/346=L)
Yes, masturbation is haram, but if anyone fears involvement in adultery and commits masturbation then it is hoped that he will not be punished fro the same:
فی الجوھرۃ: الاستمناء حرام وفیہ التعزیر (الدر المختار مع الشامی: 6/38، زکریا دیوبند، کتاب الحدود) و فی موضع آخر: لو خاف الزنا یرجی ان لا وبال علیہ (الدر المختار مع الشامی: 3/371، زکریا دیوبند)
The best way for you is to marry if you are unmarried. If you can not afford marriage then you should keep fast as much as you can. If you feel tended to masturbate recall the punishments of Allah and seek His forgiveness sincerely for such acts committed in past and resolve not to repeat them again.
Allah (Subhana Wa Ta’ala) knows Best
Darul Ifta,
Darul Uloom Deoband