Wikipedia explains a phenomenon known as in-group favoritism as follows :
In-group favoritism, sometimes known as in-group–out-group bias, in-group bias, or intergroup bias, is a pattern of favoring members of
one's in-group over out-group members. This can be expressed in
evaluation of others, in allocation of resources, and in many other
This interaction has been researched by many psychologists and linked
to many theories related to group conflict and prejudice. The
phenomenon is primarily viewed from a social psychology standpoint.
Studies have shown that in-group favoritism arises as a result of the
formation of cultural groups. These cultural groups can be divided
based off seemingly trivial observable traits, but with time
populations grow to associate certain traits with certain behaviour,
increasing covariation. This then incentivises in-group bias.
Two prominent theoretical approaches to the phenomenon of in-group favoritism are realistic conflict theory and social identity theory. Realistic conflict theory proposes that intergroup competition,
and sometimes intergroup conflict, arises when two groups have
opposing claims to scarce resources. In contrast, social identity
theory posits a psychological drive for positively distinct
social identities as the general root cause of in-group favoring
In layman's terms, in-group favoritism is a social phenomenon that consists of people associating and identifying with certain collectives and then favoring members of that collectives over people who are not a member of that collective. At the psychological level, this phenomenon can be traced back to a general need to feel part of something greater. At a higher level, it appears to have grown out of the necessity to cope with millennia of inter-group competition between different tribes, clans, etc.
Now, this "group" could be based on anything that unites people. It could be shared gender, shared religion or shared race. But it could also be a hobby you share with others. Anything that you have in common with other people can result in a group identity among members of the group and therefore in favoritism between members of that group vis-a-vis non-members of that group. Ideologues and political leaders who understand this mechanism manipulate this mechanism to create cults around a certain group of shared characteristics.
If you look at all inhabitants of a country, there's but a single trait shared between all members of that country. Thus, for the leader of said nation, membership of that nation is the one characteristic you want to emphasis to create a sense of "belonging part of a greater whole", especially if you want to unite your population against a certain enemy. This is what is known as civic nationalism. However, other forms of nationalism exist based on different criteria. Religious nationalism, for example, puts its emphasis on a shared religion and ethnic nationalism on a shared ancestry.
Whichever form of nationalism is prevalent in a region largely depends on that nation's ethnic and religious composition and the main causes of conflict between different groups within that region. However, each form of nationalism shares the same roots in in-group favoritism and political leaders using it for their political agendas not just with each other but also with eg. Feminism or Marxism, where respectively gender and class are the main characteristics of group identity.
Whichever characteristic is used as the basis of group identity, the more one's own individual identity is intertwined with one's group identity, the more one is willing to both die and kill for that group and everything it stands for! And the more fanatics any group has, the more powerful it becomes.