I can't speak to cognitive bias, but the origin of the desire to harm others is easy. It's a manifestation of our pack-social instinct. Like wolves, elephants and quite a few other species of mammal, we are programmed to form packs: small groups of individuals that hunt or forage together because it makes them more successful and/or safer. Usually a pack is an extended family that an individual is simply born into, but not always. Elephants petition to join a pack, and undergo a rite of passage, the way we apply for membership in the Rotary Club.
Predatory species tend to be hostile to other packs because their food supply is limited. If one pack encroaches on another's territory, there probably won't be enough prey to feed both of them. Humans have always been obligate carnivores, going back several million years into more than one generation of ancestral species. And since we don't have the natural hunting tools (teeth, claws, speed, strength, etc.) that natural predators use, but instead use tools and strategy, we need to be a little more protective of our hunting grounds than wolves and lions--and wolves and lions are pretty tough on outsiders!
This means that in the Paleolithic Era (the "Early Stone Age," before agriculture was invented and created a permanent food surplus), during a dry year when food was scarce, two tribes might have had to fight each other for sheer survival. Whoever got the food lived.
In fact, the examination of Paleolithic skeletons using modern instruments has discovered that more than 50% of adult humans were killed by violence. In other words, more humans were killed by other humans than by all other causes combined.
So it's a deep-seated instinct to be wary or even hostile to people outside our own community. We should be proud that we've managed to expand our definition of "community" to include people on the other side of the planet who are nothing more than abstractions.