This is a complicated question and many factors play role here. On a very basic level, people tend to label others and treat the out-group worse than an ingroup. Individuals suffering from a mental disorders are easily categorized as an out-group. Moreover, mental health problems have been poorly understood for ages, so they seemed "mysterious", "inexplicable" etc., creating a feeling of uncertanity or even fear (someone acting "irrationally" is often seen as a threat). This is a "perfect" basis for discrimination. Especially when others tried to somehow find and "explanation" for the mental illness and usually used categories of sin or "inherent corruption", used internal attributions (fundamental attributional error) - blaiming the person for their own suffering (which is caused by the faith in a just world). Another thing: some mental diosrders actually do cause some level of (temporal or more persistent) disability or at least make people vulnerable or "weaker" - which also may lead to more severe discrimination. Another answer can be found in the Scapegoat theory - due to some aspects of "mental illnness", it constitutes the perfect characteristic of a scapegoat. You can see this very clearly in the phenomenon of bullying - the victim is usually someone who stands out - negatively (e.g. a very shy person) but also positively.
"Mental illness" meets many characteristics of stigma: it is often visible (in a behaviour), perceived as a "flaw" or a "defect", may distrupt everyday life or social interactions, causes fear - is perceived as a peril (because it is poorly understood)...
I do not believe this answers your question fully - this are only some aspects that can be related to stigmatization of a mental illness. It is also important to remember that this is a vicious circle: people with mental disorders are often stigmatized which causes them to be a marginalized group, which increases stigmatisation...
Rachela Antosz-Rekucka I wonder what you make of how maybe the Enlightenment created the dichotomy between rationality and irrationality whereby anything that doesn´t conform to Descartes´mind body binary is constructed as mental versus physical/ biological.
Perhaps it is useful to read about the history of mental health. Centuries ago there were theories such as that the Devil or Satan inhabited people who were crazy, they were “possessed”. Orthat it was sin and sinful origins. Or defective genetically, a more primitive kind of human, half-animal. The basis of this could be the fear of unpredictable behaviour, shame “what will the neighbours think” or the heavy weight of supporting non-productive family members in times of poverty, scarcity and famine.
Search for a good history, in western culture and other culrures.