Life is full of competition, and therefore resentment and self-doubt, which leads to lashing out.
A big, looming question for all of us is how much of the outcome is pre-determined. [ @Alexei_Kirillov ] There's this perception that your success is a measurable calculation involving both things in your control and out of your control. Things out of your control would be socioeconomic status growing up, family stability, genetic gifts like IQ or athleticism or just being healthy, etc. What's in your control are the choices you make, what you do with your time, where you focus your effort, how much effort you put in, things like that. (Note that I don't necessarily agree with all of this. I've written that I think capacity for effort is its own sort of talent, but this is a regular perspective.)
We all can't be winners in the various competitions in life. Most of the big things we selfishly want - be among the most rich, top of your profession, with the most beautiful partner, most popular, etc. - have very limited slots. The people who feel like they came up short - whether they admit it or not - want to feel better about themselves in comparison to others. Because they're told/they believe that the non-deterministic 'things in your control'/'things out of your control' dynamic is proper, they want to tell themselves that as many of their shortcomings as possible fall into the 'things out of their control' category. They do this by comparing themselves to others.
The result is a race for the bottom. To keep it overly simple, let's say "Success" = X + Y, with X being what's under your control and Y being what isn't. You know what your estimate of your success is, so we have a guess at the sum of X and Y. You want to say that your X is the highest possible, and the only reason others did better than you is they were lucky and born with/fell into high Y. You start adding up Y factors to compare with people around your level of Success. Most can be seen (even if our perception is bad, we're biased to think it's good): 'oh you grew up in a rich household, higher Y,' 'oh you lost your leg in an accident, lower Y.' Mental health is largely unseen, though.
So say I'm making my comparison with someone in a similar circumstance to me (similar perceived "Success") and so far all the Y factors are pretty equal and what's left is our mental health issues. I have depression, and I want to argue this falls into category Y, a hindrance like missing a limb, meaning my X is higher: I'm doing the best one could hope to do with my circumstances. But now, the person I'm comparing to also says they have a mental health issue, be it anxiety or ADHD or whatever. You can see where this leads. My ego, to protect myself from having a bad self-image under the paradigm I have created, lashes out to put down the other person and protect my self-image. "Their issue is exaggerated, it's not real like mine, they're just making excuses because they aren't trying hard enough."
It's ugly, and it's cruel, and it's misguided, but it comes from a position of not being happy with themselves. Pain leads to pain.
That was a little long and maybe convoluted but I think that is part of the Ego's thought process in choosing the behavior you describe.