It was not considered "illegal" at that time and it was socially accepted. Even the worst critics of Prophet didn't bring this up. It was first pointed out in early 20th century.
Legality isn't the big deal here - it's the ethics and morality that are lacking and the herd mentality. No one dares to stand up for fear of retribution and no change is instilled. But yeah - it's easy to sit here and judge with us in a society which actually got to the point where we can talk about morality openly.
Religion isn't to be blamed. The reality is that some people are just shit and have no moral compass or ability to think for themselves or the courage to stand up for what they believe. Hence these atrocities occur.