Sins are like an ill-smelling putrid bog. The deeper a person goes into it the less he feels its disgusting smell because his smelling taste ceases to function properly. However, a person's firm decision to discontinue lusting is by itself a victory.
Masturbation has been considered to be one of the major sins; it is totally forbidden in Islam and whosoever commits this sin will be punished in the hereafter. To take practical action to stay away from this sin and to seek divine forgiveness is considered to be real and genuine repentance and it is the best way to make up for the afflictions and harms one has suffered.
The best way to get rid of masturbation is marriage in accordance with the rules. If he cannot afford marriage, he should take exercise, keep fast and fill his leisure time doing useful work so that he may protect himself and the society against the physical and spiritual harms of this evil act.
Sins are like an ill-smelling putrid bog. The deeper a person gets into it the less he feels its disgusting smell because his smelling taste ceases to function properly. However, a person's firm decision to discontinue lusting is by itself a victory. One who has achieved victory at this stage should try to strengthen his motive to fight sins and not to fall in them so that he may also succeed in the subsequent stages of his life.
As for how to strengthen one's motive to fight this sin, the following pieces of advice can be taken into consideration:
1. To be mindful of the greatness of God and of one's duty to fulfill His orders;
2. To respect one's own personality;
3. To be mindful of the dangers and evil consequences of sinning and lusting after someone;
4. To be mindful of the importance of protecting oneself against sins and temptations; gaining victory over the carnal desires or the temptations is a pleasant victory as well as an art.
The point which is very important in this regard is not to despair of divine mercy and that one should seek divine assistance and consign his matters to Him. If a person commits a sin again, he should not get disappointed. He should make up his mind to adopt the foregoing alternatives and options, and should fight the sins. We are of the view that you will succeed in your efforts with God's help and divine grace.
Masturbation according to religious texts means to touch or rub one's sexual organs in order to give himself sexual pleasure to the point of ejaculation or orgasm. Ejaculation can take place by way of touching the sexual organ, watching sex scenes (picture, film etc.) or studying and reading romantic books and stories. It can also take place through fancying about sexual matters or listening to cassettes or telephone talks. In any case, all of these are forbidden under all circumstances and one's deliberate action to take out his semen is considered to be a great sin.
Without doubt, taking practical action to stay away from this sin and to seek divine forgiveness is considered to be real and genuine repentance and it is the best way to make up for the afflictions and harms one has suffered due to committing this sin.
However, the best way to get rid of the evil consequences of masturbation is marriage in accordance with the rules. If one cannot afford marriage, he should make act on of the following points with reliance on God:
1. You must make your mind firm to avoid lustful actions.
2. Do not watch sexually provocative and exciting pictures and films.
3. Keep away from bad company and make sure to choose a good friend, one who does not encourage you to commit sins. Do not make friends with anyone from the opposite gender.
4. Keep away from fancying and always engage in useful work (e.g. reading books, taking exercise etc.) and bear in mind that being idle is very dangerous and if you are idle, then you are likely to be led to do any sinful action.
5. Try as far as possible not to be alone at a place.
6. Fasting is very important for controlling your physical desires and strengthening your will. If you cannot observe fast, make sure not to overeat and your stomach should not be full before going to bed.
7. Do not eat the food that causes sexual excitement. Do not eat bananas, chocolates, figs, dates, onions, pepper, eggs, beef, oily foods etc.
8. Always keep your bladder empty.
9. Read something before going to bed. When going to sleep, you must be completely tired and sleepy. Do not sleep unless you feel that you need to sleep. Also, do not sleep with your face down because you will feel sexually excited.
The conclusion is that staying away from being alone and filling your leisure time doing useful work will be greatly helpful as they can protect you and the society against the physical and spiritual harms of this evil act. Stay in touch with faithful and believing people, and take action for marriage, if possible. By doing so, you will be able to remain immune to sins., 
 - For further information vide: question 370, (site: 379) (Repentance)
 - In some traditions, this sin has been considered equal to adultery. See: Wasail al-Shia, vol.20, pg.352.
 - Excerpted from question 883 (site: 962)
 - excerpted from question 1028 (site: 1083)
 - For further information see: question 883 (site: 962)
 - Extracted from index No.3235.