I see religion as a tool that comes from mutual collectivism. I would guess it came about to make people feel better than opposing tribe,closer to tribe, increase likelihood of passing down information in previous environment, special, killing other opposing tribe, feel better about killing animals etc. I think religion in general effects behavior much less than most people think, as it's function is not to actually change your life but make you feel superior to opposing non part of your religion in some ways. If you look as studies religion and non religious people generally have similar rates of teen pregnancy, drug using or whatever metric you want to use. Christians do not actually appear to generally follow their own teachings, etc. Keep in mind like all traits a sprectrum, with religion as a trait certainly not perfect, it's a very flawed trait that reflects how it came about by chance.