Praise be to Allah.
The secret habit is haraam, as we have explained in the answer to question no. 329, q.v.
You have to repent from this action by giving it up, and regretting what you have done, and resolving not to go back to it.
The way to treat this bad habit is to understand the devastating damage that it causes and to follow the advice given by sharee’ah.
With regard to the harm caused by this secret habit, researchers are agreed that it is harmful, and that it increases and gets worse the more a person – whether male or female – does it. Researchers have written a great deal about the psychological, intellectual and nervous damage it causes, let alone the physical damage to the reproductive system, the digestive system, the heart, the faculties and the body in general.
It is a good idea to refer to some of the things that have been written about this habit.
Quoting from the book al-‘Aadah al-Sirriyyah ‘inda al-rajul wa’l-mar’ah wa adraaruha (The secret habit among men and women and the harm it causes) by Muhammad Faa’iq al-Jawhari:
This is in addition to the negative effects it has in the spiritual realm – which is not mentioned by most researchers – of causing the person to neglect prayers because of the need to repeat ghusl, especially during the winter; and weakness of resolve in resisting the means of immorality and provocation. In addition to that there is the sense of psychological defeat which leads some people to forsake righteous friends, because they feel that they do not deserve to be in their company, in addition to it being one of the means of taking lightly immoral actions such as zina and homosexuality.
With regard to shar’i advice, we advise the following:
1 – Lower your gaze and avoid looking at haraam things. That includes not looking at women on the TV, for the pictures of women that are shown on the TV are one of the greatest causes of fitnah (temptation) and provocation of desire. Forbidden looks are one of the arrows of the Shaytaan. If you are fulfilling your desire by means of the secret habit now, you will not be able to control this desire and prevent yourself from satisfying it by means of other major sins that are even more serious, such as homosexuality and zina.
2 – Avoiding being alone. The wolf only eats the sheep that strays away from the flock, and the Shaytaan gains control over the man who is on his own, but he is further away from two and even further away from three.
3 – Fast a great deal. This is the advice of the Prophet (peace and blessings of Allaah be upon him) to young men who are unable to get married. Fasting is a way of disciplining oneself, lowering the gaze and guarding one's chastity.
4 – Making a lot of du’aa’, praying for forgiveness and glorifying Allaah. Whoever keeps himself busy with this, the Shaytaan will have no way to tempt him and lead him astray.
5 – Praying sincerely that Allaah will keep you away from haraam things and whatever leads to them. Allaah says (interpretation of the meaning):
“And your Lord said: Invoke Me [i.e. believe in My Oneness (Islamic Monotheism) and ask Me for anything] I will respond to your (invocation)”
6 – Keeping company with righteous people. A righteous friend will only show you the way to good. ‘Ali ibn Abi Taalib (may Allaah be pleased with him) said: “It is enough honour for my brothers that I do not disobey Allaah among them.”
See also the answer to question no. 3633, which explains the ruling on watching TV. You can also see the answer to question no. 20229, which explains ways to help you lower your gaze; and 20161, which explains how to solve the problem of desire.
And Allaah knows best.