I have never liked the idea of accidents or by-products in evolution. I tend to think that evolution does not produce accidental traits or if it does such things do not survive long, I am not sure. But in any event it seems to me that something is only an accident or by-product if we cannot figure out how it has any evolutionary value. This, I think, is essential, it is how we perceive and/or understand a thing which makes it an accident or by-product. If we cannot see how religious thought or a sense of beauty in the world can contribute to the survival in such a way that it they would become common traits then we seem to think that they may just be accidents.
Like, for example, the idea is that consciousness was a development that was beneficial to survival, larger brain and all. But consciousness just brought with it all this extra, unnecessary stuff like a sense of beauty and religion. I don't see it that way, I see the development of consciousness as leading to developments in and of consciousness that will benefit survival. Seeing beauty in the world may increase the desire to live and make a person fight harder to stay alive, survive, and reproduce. If we couldn't see beauty our consciousness might lead us to see everything in the world with fear and/or anger or some other negative emotion (that would have been more common perhaps in the instincts of or ancestor species). This would lead to seeing nothing worth living for thereby reducing survivability by promoting an attitude that there is no reason to fight to stay alive because life just sucks. So beauty, emotions, religion, and all these sorts of things that tend to be seen as accidents or by-products may just be beneficial traits subject to natural selection.
And so I don't think religion was an accident of evolution. I think that it is the precursor to a more fully developed sense of reality, like a primitive eye that is nothing more than a light sensing cell. Religious thought allows us to sense the light without being able to see it. As we continue to evolve our sense of religion ought to allow us to see God more clearly. It is like asking if the lobed fins on a fish where an accident, if they where no fish would have been properly equipped to crawl out of the oceans. Consciousness, in evolutionary terms, is a trait that has not been around long and so is in its very early stages of development. Who knows where it will lead or what ideas will survive and what will be discarded along the way...