Al-jawab billahi at-taufeeq (the answer with Allah's guidance)
1) This is a form of masterbation.
2.a) You must struggle against your Nafs in doing good deeds and obeying Allah Ta'ala and His Messenger (Sallallahu Alaihi Wasallam). Allah Ta'ala says, "Those who strive hard in our cause (worships), We will guide them in our paths (to the Deen), Indeed Allah is with those who do good deeds." (45:69)
For example, When a person finds it hard to stop this habit, he must struggle against his Nafs and desires (of lustful thoughts) trying to control and conquer it, then Allah will open and guide him to the paths and make it easy for him to live according to Quran and Sunnah. Indeed He is with such people.
b) Find the root causing you to fall into this sin and eliminate it, i.e. lower your gazes. Allah Ta'ala says,
“Tell the believing men to lower their gaze (from looking at forbidden things), and protect their private parts…” [al-Noor 24:30]
Lowering the gazes brings light to the heart, just as letting the gaze wander brings darkness to it.
c) Increase your Imaan. Please view this link on ways to increase Imaan: http://www.muftisays.com/qa/question/1779/guidance.html
d) Keep active in doing something good; such as, acquire Ilm (knowledge) about one's Deen.
e) Stay in the Suhbat (company) and Majalis (gatherings) of the pious people.
f) Make a routine of reciting the Quran like morning and evening etc.
"We sent down in the Quran that which is a healing and a mercy to those who believe." (17:82)
Recite Zikr abundantly.
“O you who believe! Remember Allah with much remembrance”. (Ahzab:41)
Recite La Ilaha IllAllahu Muhammadur Rasulullah frequently.
Recite Surah Yaseen in the morning.
Recite Muawwazatain (Surah Naas and Falaq) and Surah Ikhlas and blow over yourself from your nape to feet 3 times, after every Fardh Salah and before you retire to bed.
g) Do not lose hope in the Mercy of Allah Ta'ala and His forgiveness. Allah Ta'ala forgives all sins. If you are sincere in your repentance, verily Allah Ta'ala will forgive you, as He is Most forgiving, Most merciful.
Allah Ta'ala says : "Say: 'O My slaves who have transgressed against themselves (by committing evil deeds and sins! Despair not of the mercy of Allah; verily, Allah forgives all sins. Truly, He is Oft-Forgiving, Most Merciful'. (39:53)
"Pray to Him with fear and hope; His Mercy is near to the righteous." (7:56)
"Do not despair of the Comfort of Allah, none but unbelievers despair of the Comfort of Allah." (12:87)
And Only Allah Ta'ala Knows Best.