Sometimes it just happened. Sometimes he simply lost control, got lost inside his mind. Deductions came to him, unstoppable, and when he hid from the world to make them stop his mind would just move on to philosophy or chemistry. Filling his mental faculties with uninteresting details about everyone's petty problems, or great insights about society that nobody but him would be able to understand.
He'd learned not to flee from these kind of thoughts, though, no matter how useless they felt. Silencing them led him to the more personal rooms of his mind palace, the ones furthest from the main hall, usually carefully locked. The rooms where he stored everything he tried to delete long ago, but simply couldn't. Unpleasant childhood memories, insecurities, that one time the price for not finishing an investigation would have been John's life. No, to him feeling useless was preferable to feeling anxiety, shame and sadness, leading him to avoid silencing the useless thoughts.
So when his mind wasn't occupied with something of significance, like Lestrade's cases or his experiments, Sherlock was doomed to go through the same process every time. First he would involuntarily deduce everything he could from everyone in sight, till his eyes were too tired to observe more cheating husbands, more abused children or more terminally ill who were not aware of it yet. Till his mind was too full of information he couldn't use. He would then retreat to 221b, not accepting any visitors, and fail to distract himself with things others would destract themselves with, like television. His mind would wander off to impractical theories of human existence, glimpsing at the locked doors every now and then. During these periods he neither ate nor spoke, the combination of his overactive mind and the lack of nutrients draining him of all energy to go do something, pulling him even deeper into his depression. Now, if luck was on his side, this was where the process ended until something exciting happened. Sometimes, though, his mind wasn't satisfied with making him feel useless, blasting open the locked doors in the back of his palace and pushing him inside. Driving him absolutely insane. Making him willing to do anything for it to stop.
Before, "anything" meant sniffing coke. It finally silenced everything, instead of replacing one unpleasant thought with another. The effect was of course temporary, followed by an immense drop, leaving him needing more. Mycroft stopped that vicious circle by putting him in rehab. Mycroft got him off the drugs. But Sherlock stayed off the drugs for a different reason. Sherlock stayed off the drugs because he met John.
John distracted him sufficiently, sometimes on purpose, sometimes just by being there. No more drugs needed. After contemplating for hours why John was able to help him this way, he finally reached a conclusion that should have been obvious from the start. When everything became too much, he could just think about John. And John, well, there was just simply no negative way of thinking about John. His John.