The virgins were not killed in Numbers 31:17-18 because they did not participate in the sin of Baal at Peor. Let's take a look.
"Now therefore, kill every male among the little ones, and kill every woman who has known man intimately. 18 But all the girls who have not known man intimately, spare for yourselves," (Numbers 31:17-18).
The Midianites were descended from Abraham and Keturah (Gen. 25:1). They inhabited the land of Moab and were apparently involved in seducing Israel into going after false gods. Because the Israelites fell into idolatry this way, God told Moses to order the deaths of all who had bowed to the false gods in that land.
"While Israel remained at Shittim, the people began to play the harlot with the daughters of Moab. 2 For they invited the people to the sacrifices of their gods, and the people ate and bowed down to their gods. 3 So Israel joined themselves to Baal of Peor, and the Lord was angry against Israel. 4 And the Lord said to Moses, "Take all the leaders of the people and execute them in broad daylight before the Lord, so that the fierce anger of the Lord may turn away from Israel." 5 So Moses said to the judges of Israel, "Each of you slay his men who have joined themselves to Baal of Peor," (Num. 25:1-5).
God later instructs the Israelites to deal harshly with the Midianites: "Be hostile to the Midianites and strike them; 18 for they have been hostile to you with their tricks, with which they have deceived you in the affair of Peor, and in the affair of Cozbi, the daughter of the leader of Midian, their sister who was slain on the day of the plague because of Peor." (Num. 25:17-18). Later, when Moses meets the returning Israeli army he was angry because he saw the Medianite survivors. "The Midianite women, he said, should have died because they were directly culpable in Israel’s sin at Baal of Peor. All the women except the virgins were then sentenced to death along with all the boys. This insured the extermination of the Midianites and thus prevented them from ever again seducing Israel to sin...The virgins were spared because they obviously had had no role in the Baal of Peor incident nor could they by themselves perpetuate the Midianite peoples."1
Some may object that the Israelites then married the virgins, the daughters of those whom they had killed; and that this would be a horrible thing for the virgins. Perhaps it was a horrible thing for them. But, their lives were spared. Also, in that culture at that time, warfare and plunder was a necessary evil. The reality of taking women as wives was unfortunate but true.
Why was God so harsh with those in idolatry?
We must understand that God dealt very harshly because it was through the people of Israel that the Messiah would later come. Satan, in his perpetual effort to oppose God, sought to have the people of God fall into false worship and through intermarriage with other people to destroy the messianic line and make not only the promises of God null and void but also destroy means by which the Messiah could be born. If this could be accomplished, then none would have any hope of deliverance from sin. Therefore, we see in the Old Testament God being very harsh and strict according to the Law.
- 1. Walvoord, John F. The Bible Knowledge Commentary, Nu 31:13, Wheaton, IL: Victor Books, 1985.