Islam never overlooks human desires and instincts. In Islam, fulfilling the sexual desire is done through the marital relationship. So, our first advice to whoever is addicted to such acts is to seriously repent to Allah and get married as soon as possible to a righteous Muslimah.

A Muslim should never watch pornography, and should do his best to control his sexual desire.   He should also dissociate himself from whatever leads to such sins.

Responding to a question of this type, Sheikh AhmadKutty, an Islamic scholar, stated the following:

I pray to Allah to grant you patience (sabr) and keep your mind away from sin, and may He bless you with marriage as soon as possible.

In the meantime, you are best advised to turn your mind away from lustful thoughts by occupying yourself with other beneficial matters. You should ask Allah to help you. Here are some suggestions I can give in order to guard yourself against falling into sins:

1. Make it a habit to read the Qur’an or listen to a beautiful recitation.

2. Practice remembrance of Allah (dhikr). Find a good prayer book such as Muhammad Al-Ghazali’s Remembrance and Prayer: The Way of the Prophet and read it regularly.

3. Learn to curb your carnal desires. Get Al-Ghazali’s book Disciplining the Soul and Breaking the Two Desires (translated by T.J. Winter).

4.Find good companions who will divert your attention to good things.
5. Find volunteer services that need help in your community and spend time volunteering your efforts.

By keeping your mind occupied with positive and beneficial things, with the help of Allah, you can guard yourself against sinful thoughts.

Finally, use the following prayer on a regular basis:

Allaahumma tahhir qalbee wa ahsin farjee.

(O Allah, cleanse my heart (of all sinful thoughts) and help me to remain chaste and pure).

Rabbi qinee sharra basaree, rabbi qinee sharra sam`ee; rabbi qinee sharra lisaanee, rabbi qinee sharra maniyyee.

(My Lord, guard me against the evils of my eyes; my Lord, guard me against the evils of my ears; guard me against the evils of my speech; guard me against the evils of my private parts.)

Allaahumma habbib ilaynaa al-imaana wa zayyinhu fee quloobinaa wa karrih ilaynaa al-kufra wa al-fusooqa wa al-`isyaana.

(O Allah, make faith dear to our hearts and embellish our hearts with it; and make disbelief, transgression and rebellion abhorrent to our hearts.)

Allaahumma aghninee bi halaalika `an haraamika wa bi ta`atika `an ma`siyatika wa bi fadhlika `amman siwaaka.

(O Allah, make me self-sufficient with what You have declared as lawful for me; make me self-sufficient with Your obedience so that I am not driven to disobey You; and make me self-sufficient with Your favor so that I am not compelled to look to favors of others.)