Well back in the day, religion and national identity were often one and the same. The tribes of Israel are a great example of this. For example here, the concept of a cosmic duality (a good diety such as God and an evil element such as the devil) is something that is quite recent in human history. From what I can remember, and I'll double check this in the morning when I'm thinking with a more clear head, Zoroastrianism was the religion that got that whole ball rolling. It was when the Jewish tribes came into contact with Zoroastrians that the idea of a cosmic duality was introduced into the Abrahamic Faiths. Before then, the belief that there were many gods was very common. In the Ten Commandments for example, God (capital "G" as in the one main God as far as Jews, Christians, and Muslims are concerned) said "Thou shalt have no other gods before me." What he's saying is, you can worship other gods if you want, but you need to understand that I am the TOPS. In the Middle East, there were many tribes and many of them worshipped their own gods. The gods they worshipped tied into their national identity. If you want a peek into how the religious beliefs of these tribes affected their concept of national identity, check out this article on The Moabite Stone. What's really interesting about people who choose to congregate over shared interests, whether they be something philosophical like religion or the lack there of, hobbies such as gaming or ham radio, or professional such as medicine or engineering, is that they all eventually develop codification in everything from jargon to accepted beliefs. It creates a shared culture that becomes so strong that to go against what is considered group norms brings about the risk of being ostracized. When I was in college, one of my religious history teachers was talking about how both atheist and non-denominational Christians groups (two more recently developed religious subgroups in America)have largely homogenized over the course of a few decades.I'd like to comment a bit on religion as an identity. Especially in the modern practice of religion, this is immensely important. I have no idea what factor it played waaaaay back in the day but today it is probably the main reason people join or continue to practice religion these days.
Even the hardcore atheists - the ones who preach atheism and go to atheism meetups - seem to be more and more like a religion these days.