This happens because of Rumination, and in Psychology proper specifically within the Default Mode.
The gist of it is that when your mind wanders, it naturally wants to wander to the thoughts and feelings that are unknown/unsafe and the relativity of negative feelings that come along with it.
On a level of evolution and growth, humans are by default social and emotional animals.
However, we have the ability to develop critical thinking if we do some critical thinking exercises, and the more we do these exercises, the more we cognitively develop.
Cognitive development through education, be it traditional education or through self teaching, can help teach us about ourselves and about our interests.
The utilitarian application of this is to try to develop more cognitively-demanding things to think about than you have of emotional things to think about.
I liken it to growth, because children have little to no cognitive development due to their age, inexperience and immaturity. They aren't that developed yet.
But over time, as they grow, the trajectory is that they go to school, learn, and eventually go to work, and apply what they've learned.
It's an endurance thing, similarly to lifting weights or exercising physically.
Big results and changes come with long amounts of time, in the beginning there are only subtle changes.
I've been in the practice of this for about 10 years now.
It's actually become vitally important to me on a personal level, because it teaches me to be in better control of my mental state.
The idea being that if I'm in enough control of my mental state, and I've got enough field experience dealing with life stress, nobody can use my emotional state against me, and I can teach myself to be strong through harder times.
Now obviously, this isn't something that can be upheld or turned into a state of permanence, also through evolution our brains simply didn't evolve that way, that is of literal physical evolution in that we evolved to speak and have oral communication whereas chimpanzees actually display superior nearly photographic memories on a much more common rate. Chimps don't speak, so they memorize super fast and super well. Humans speak though, and the suggested neuroscience reason as to why we evolved to speak is that our brains process information at higher and differential rates by comparison. Basically making our brains be like: "Uuuhh, hold my beer for a sec, I gotta take this call."
And of course to make a joke about it we all know how "hold my beer" usually goes.