The Hebrews were at war with the Midianites. All men and married women ( the unmarried women were supposed to be killed during battle too but the soldiers disobeyed by taking them captive - meaning that it was not commanded by God that they do so) were killed as a part of that war. Specifically because they were guilty individually and as a people of sin so that their deaths were deserved. The passage makes a clear distinction between spoils of war and captives so that MT's characterization of them as spoils of war would be completely opposite of what the passage said. Additionally, the assumptions that as captives they were raped is complete conjecture on the part of those who say that it happened. Given the context of the laws for treating captives if the Hebrews followed those laws then they were treated well. They would only have been raped if other laws were violated. We can only believe they were raped if we ignore other laws, laws that are given in earlier passages which make it illegal to rape and illegal for Hebrews to marry (or probably even to produce children with) Midianites.
Paraphrased: ...all have sinned and come short of the glory of God... Including the Hebrews. Why then were their sins overlooked and the sins of the male babies of the Midianites were not? What sin had those babies committed? Does who wrote the story (Hebrews or Midianites) have any bearing on the justification?
Taken in the context of the time where it was not adultery or rape to have a child with your wife's handmaiden, and the context of "...take all females who have not known man for yourselvels..." Just what do you think that they were going to do with them if it was not for sexual purposes? Of course, the Hebrews would not describe it as rape, they wrote the story and thou shall not commit adultery had not been invented yet.