Masturbation is haram (forbidden) and no one can commit this sin under any pretexts. Those who cannot afford marriage must try to take exercise, observe fast, keep their pubic hair long, avoid eating energetic food, looking at non-mahram, fancying or thinking about sexual issues and being alone. By doing so, they can weaken the sexual urge and do away with all kinds of the preliminaries of masturbation.
In order for this person to get rid of this sickness, he must make up his mind and pray to God to help him abandon this evil action. Obviously, masturbation is a great sin and the doer has been reproached by the Quran. He who commits this sinful act will be liable to divine punishment. As for what kind of punishment he will be subjected to in the hereafter, no one knows except God, the Exalted.
There is no special dua (supplication) for quitting masturbation; any dua in any language which contains an invocation to God for protection against this sin would be good.
Meanwhile, God has promised to forgive the sinners who repent, seek divine forgiveness and is determined not to commit it again. However, he who does not regret his sin and does not seek forgiveness must not expect God to forgive him. Indeed, it is possible that sometimes a person has regretted over his mistake and repented genuinely but he has been tempted by Satan and made to commit the same sin again, in which case, it is necessary for him to repent once again. In any case, you should never despair of God's mercy and you should know that even if you break your tawbah (repentance) a hundred times, the gate of divine mercy is still open to you.
Answers by the Offices of Some Grand Jurists
Office of Grand Ayatollah Khamenei (may Allah grant him long life):
It is forbidden and one's being unable to marry does not become a license for masturbation. Sometimes, quitting a sin may look a bit difficult in the beginning but you should be mindful of the dangerous consequences that you might face in this world and in the hereafter. You should know that masturbation not only affects your health but it will lead you to misery in both this world as well as the hereafter. Therefore, you should abandon this great sin by making up your mind and relying on God and divine help. Hence, you should stay away from watching, hearing and reading sexually offensive materials. Also, you should avoid associating with bad friends.
Reciting the holy Quran regularly, attending religious gatherings, doing tawassol to the Ahlul-Bayt (a.s), acquiring education, continuous exercise, engaging in useful jobs and arts like calligraphy, painting, industry etc. remembering death and the Day of Resurrection are helpful in this regard. If you have not married, try to take action for a simple, economic and healthy marriage so as to save your faith as well as your body from the consequences of this evil action.
Office of Grand Ayatollah Bahjat (may Allah grant him long life):
Masturbation is absolutely forbidden and it is necessary to repent for it.
Office of Grand Ayatollah Fazel Lankarani (may Allah grant him long life):
It is haram and he must do Ghusl-e Janabat and repent.
He should repent genuinely so that God, the Exalted, may forgive him.
Office of Grand Ayatollah Makarem Shirazi (may Allah grant him long life):
Masturbation is forbidden and it is considered to be one of the major sins, and it has also a lot of evil effects for the doer.
If you avoid watching offensive movies, books, magazines, going to bad parties, being in bad companies and seeing sexually exciting scenes and fill your leisure time reading books, doing useful work and taking exercise, God willing you will overcome your carnal desires and physical temptations and you will finally succeed in quitting masturbation. Make sure to rely on God. We pray to Almighty God to grant you success.
For further details, see "The Sexual Problems of the Youth".