TW: Intrusive terrifying thoughts, themes of rape and child abuse

My best friend Mark (21M) has depression and has had it since he hit puberty. Depression runs in his family, and he seems to have developed it very suddenly with no trigger. I met him a year after this and always knew him to easily shut down and sleep for very long periods of time with no clear reason - breaking communication suddenly only to suddenly start responding to messages again. I recognized even then that something was wrong, but I wasn't sure what. 

After we had known each other for a few years that early in his depression he had had frequent intrusive disturbing sexual thoughts that made him disgusted with himself. He frequently had involuntary and sudden thoughts of violently raping random people he knew - something that he found no enjoyment in and actually made him suicidal. Mark is the most gentle and compassionate person I know, and he isn't actually interested in sex at all. Even though the information was somewhat shocking to me at the time I didn't for a second think that it was in any way reflective of his desires. He still felt intense guilt over it even though the thoughts had gone away and been replaced with infrequent disturbing dreams. I couldn't reason what it was at the time but told him I thought it was some kind of mental trap. Disturbing thoughts multiply when you let them take space in your mind, even if it is unintentional. It's not something he could control, but was probably made worse by the fact that the thoughts were traumatic experiences, hence his brain would repeat the imagery. I have no idea whether that's true, that's just what I thought made sense at the moment and I figured it might make him feel better reasoning that it was out of his control. 

Recently I was doing unrelated research on OCD and saw that a very common symptom was obsessive sexually disturbing thoughts, such as raping someone, being raped, or abusing a child in a sexual way. The person experiencing these thoughts in this case does not want the thoughts, and they can be traumatic or cause suicidal ideation and self-isolation as a result of someone fearing themself. It sounded exactly like what had happened to Mark, and I wondered if it was a symptom of depression as Mark has none of the other symptoms of OCD. Turns out that it is, albeit a more uncommon one. I was wondering whether anyone had experiences like these or knew the causes of this. Mark tends to feel much better about things like this when he understands them - just realizing that he had depression helped him a lot so I feel like an explanation of this thing that happened to him almost a decade ago would help a lot.