It is well known in evolutionary theory that the question whether some trait X is a byproduct or an adaptation is not fixed once for all. The same applies to cultural entities or traits.
One thing we can say with some confidence is that if religion did started as a byproduct, this does not mean that it stayed that way. Indeed, if it had no function or even a slight negative function, it would be unlikely to persist for long. Well known in evolutionary biology is the move to a positive function for which a feature was not originally designed—what have been called exaptations
Whatever the function or not of religion as it began, it could be picked up quickly by natural selection and turned to other uses. And if Durkheim and the other social scientists who think his way are even half right, the use is obviously some kind of group cohesion, where people benefit from being part of a society and not individuals alone.
But it would be false to claim or assume that something that has been useful - an evolved adaptation - has to stay that way forever. Something can start as a byproduct, then become an adaptation and later turn out to be useless or even detrimental after the environment has changed.
As for religions, the case is far more complicated than many of those who have written about it have argued. My impression is that there is some good evidence that religious beliefs and practices started as a byproduct of human cognition and emotional needs, but then became an adaptation or exaptation (otherwise it would be difficult to account for (a.) the long and intensive symbiosis between religious ideas and human societies, and (b.) the rich cultural evolution of religions, from simple beliefs about ancestors and spirits to complicated doctrinal systems like Roman Catholicism).
The situation today is fairly complicated, depending on three different aspects:
The answer to the question "Is religion useful, useless or detrimental?" depends on the answers to these three questions. Everything else is simplistic and should not be taken seriously.
When religion is concerned, one-size-fits-all answers simply do not work.