After the successful defeat of the Midianites, the troops return to the camp with a bountiful booty, including the women and children. However, seeing them outside the camp Moses becomes incensed. He reminds the Israelite commanders about not so recent plague which, in his words, was brought upon the children of Israel by the very Midianite women, namely the slain Cozbi.
Moses orders the killing of all women, who, as Torah puts it. “who has known a man intimately” but instructs to spare the virgins. This, according to the rabbinic interpretation, means not just virgins as such, but those who are not yet incapable of sexual intercourse, .i.e. girls below the age of three.
Talmud in Yevamot 60b helpfully explains the process by which such girls were determined: “From where did they know whether a particular girl was already three years old and fit for intercourse? Rav Huna bar Bizna said that Rabbi Shimon Ḥasida said: They passed them before the frontplate of the High Priest. Any girl whose face miraculously turned sallow, it was known that she was fit for intercourse, and any girl whose face did not turn sallow, it was thereby known that she was not fit for intercourse”.
The virginity per se would not have helped Midianite girls, since anyone over the age of three was killed anyway, just as, according to the orders of Moses, were all the boys, regardless of their age.
Sifrey Matot asks why the word “to kill” is repeated twice in this sentence.
The answer is very clear – the phrase mentions three categories of captives – boys, women, capable of sexual intercourse, girls, incapable of such.
“So says R. Ishmael. For when I read, “kill every male child, and every woman who can lie intimately with a man… And all the young girls…” I would not know whether to kill them [the women of the first verse] with the males or allow them to live along with the young [girls]. That is why it says [at the end of the verse] “you shall kill.
So the second “kill” is there just to make sure that those not worthy of life will not be accidentally spared.