You raise an important, difficult question! I’ll try and answer it, and, at the same time, tell you how I went about finding an answer. None of this is rocket science, you can do it, too!
Frist, I looked up the precise wording of the verse in your question: the original Hebrew expression that both the NIV and the NASB translate as “girl” is composed of two Hebrew words, which mean “female” and “child”. Girl is of course the obvious and correct translation for that! The Hebrew expression does not by itself allow us to specify how old these girls might have been. However, the text itself adds one more qualifier, i.e. that the girls ought to be virgins. This of course raises the question of why this should be so.
What I did next is turn to my preferred guide to Old Testament commentaries, i.e. Tremper Longmann III’s Old Testament Commentary Survey (Baker Academic, 2013). There, I had a look which commentaries on Numbers he recommends. I found that I already owned one for his “five star” rated commentaries, viz. R.D. Cole’s commentary on Numbers in the New American Commentary series (Broadmann, 2001). Turning to his commentary on the section to which your question relates, I found the following:
“Holy war had as its purpose the eradication of all impure elements from the geographical region or ethnic territory placed under the ban. Coming on the heels of an idolatrous and adulterous affair at Baal Peor involving Israelite and non-Israelite participants, a cleansing of the camp was in order so that the sanctity and purity of the community might be maintained (5:1–4). The violence of war brings death in its most heinous and comprehensive forms, rendering the combatants in a state of ritual impurity. Therefore anyone who comes in contact with the dead in an open field of battle, or within the tent of one’s enemies in the pursuit of fleeing armies, must endure the process of ritual purification for the dead as outlined in 19:11–19. The impurity of death was a serious issue in ancient Israel, for anyone who failed to be cleansed was subject to the penalty of death, that of being totally cut off from the community of faith (19:11, 20). Such impurity made it necessary for Moses, Eleazar, and those clean persons who were dwelling within the holy camp to exit the encampment and meet the warriors and officers outside the camp so that any contaminants they might have been exposed to during the campaign would not be brought into the camp.
Deuteronomy 20:13–14 prescribed the killing of all the males in an attack on a city but allowed women, children, livestock, and various commodities to be plundered by the warriors. Moses, however, was angry with his military leaders and dismayed that the Israelite warriors returned with so many women among the spoils of war. He protested their actions, decrying the fact that it was primarily the Midianite women who had followed Balaam’s counsel by leading the Israelite men into idolatry and adultery, both of which were punishable by death. So he gave orders to slay all of the males, even the young boys, and any of the women who had engaged in sexual relations with a man. Ashley suggested that God ordered the young men to be executed “in order to destroy the means of future rebellion in Midian, and that all the women who were capable of sexual intercourse be killed in order to cut off the future population and to emphasize the nature of the sin of Baal-Peor.” Women who had known men sexually, whether Midianite or sinful Israelite men, were to be considered unclean, since they were the main instrument of Israel’s demise at Baal Peor. Only the young girls would be allowed to live so that they may be taken as wives or slaves by the Israelite men, according to the principles of holy war (Deut 20:13–14; 21:10–14). By this they could be brought under the umbrella of the covenant community of faith.”
There’s a lot in Cole’s commentary to chew on and mull over. It doesn’t answer all the questions which we might bring to the text, but it is a very helpful start.
To your specific question about alleged “sex slaves”: Israelite men were expressly forbidden to take women of conquered peoples as sex slaves. If they developed feelings for such a woman, they had to give her time to mourn her family, and then they could marry her, i.e. take her as a wife with all rights and responsibilities. If they should later want to divorce the woman, she had the same rights as any Israelite woman, i.e. she could be divorced but not sold as a slave, she had to be given freedom (Deut 21:10-14).
Now, we know from Jesus that Moses’ commands about divorce do not reflect the loving, good and perfect will of God, but are a concession to the hardness of human hearts (Matthew 19:8). So, while it’s correct to point out that the practice here described in the Old Testament was not a case of sex slavery equal to, say, the way ISIS has been treating Yesidi girls, we also should not defend the regulation and practice as reflecting the perfect will of God.
The standards that Jesus lived by, and calls us to live by as his followers, are much higher than that: love your enemy, pray for those who persecute you!
Does that give you a little food for thought and prayer?