Masturbation is prohibitively disliked and so it is sinful to engage in. It has many personal and societal ill-effects that are known and recognized in sane traditional societies and by balanced people. The early Muslims used to say, “The one who weds his hand is accursed.” [Ibn Humam, Fath al-Qadir]
A person needs to take all the means that scholars mention about controlling one’s desire, including:
– Guarding one’s gaze;
– Asking Allah continually to free him from this problem;
– Involving oneself with acts of worship, remembrance of Allah, etc, because the one who longs for the Infinite Beauty of the One turns to none else;
– Removing the impermissible and doubtful from his life, especially in terms of food, money, and household “family sins”, such as television, free mixing, etc
– Whenever feeling overwhelmed with one’s physical temptation to fulfill a lawful lust (food, etc.) to calm down his nafs.
Whenever a bad thought occurs to you:
a. Seek Allah’s forgiveness and mercy
b. Seek protection from the devil by saying “a`udhu billahi min ash-shaytan ar-rajeem.”
c. Remove the things that result in that bad though immediately. So, if you see an image on a computer that arouses a desire, turn it off.
d, Thank Allah, wholeheartedly, for giving you the ability to overcome your desires by saying “Alhamdulilah”, as this makes Shaytan despair, and strengthens one s resolve.
If you keep erring, keep repeating these quick and simple steps. Make supplications with your prayers that Allah protect you. If you keep turning to Allah sincerely, He will accept your entreating, and free you of these problems.