All perfect praise be to Allaah, The Lord of the Worlds. I testify that there is none worthy of worship except Allaah, and that Muhammad, sallallaahu ‘alayhi wa sallam, is His Slave and Messenger.
Although masturbation is prohibited according to the majority of the scholars, some of them stated that there is a concession regarding it in case of necessity and need, such as for medical purposes and the like. Ibn Taymiyyah was asked about masturbation and he said: "As for masturbation with the hand, it is prohibited according to the majority of scholars, and this is the more correct of the two opinions held by the Hanbalis, and its doer is to be reprimanded. The other scholarly opinion suggests that it is merely disliked, and not prohibited. The majority (of scholars) do not permit it either for fear of fornication or otherwise. It has been reported about a group of the Companions and Successors (Taabi‘een) that they permitted it as a concession due to necessity, like for fear of falling into fornication if one is not protected from it except by masturbating. Imaam Ahmad and others also said that it is permissible for fear of illness. But without necessity, I do not know anyone who permitted it as a religious concession."
Kashshaaf Al-Qinaa‘ (a Hanbali book) reads: "It is prohibited for a man or woman to masturbate without (valid) need and he or she should be subject to Ta‘zeer (discretionary punishment) because it constitutes a sin. If one masturbates for fear of falling into fornication or sodomy or for fear of any physical harm, then he or she bears no sin for it. Mujaahid said: They used to command their boys to fulfill their need by it (masturbation)."
As for the condition you referred to in the question, we found on websites that masturbation is one of its causes, not one of its remedies. Anyway, it should be noted that physicians, and not Islamic jurists, should investigate the relationship between masturbation and this illness.
Allaah Knows best.