There is evidence that laws and (formal) religion evolved together. Similar social pressures were involved, due to needing clear rules in order to function as a large-scale society. Both involved writing, which in most times and places only a limited number of people knew how to do, thus there was a lot of crossover between religion and government. Both reflected the moral values of the people at the time that they were written, at least to some extent (people could be manipulated or pressured into following things which most didn’t really believe in, but only to a point). It’s a chicken-and-egg issue as to which came first, and might have different answers in different parts of the world.
In modern times you can have one without the other, and most nations try to minimize the crossover between the two.
Also, not only does religion not necessarily prevent slavery, it can and has made slavery far easier.