“And Moses said to them, ‘Have you spared all the women? Behold, these caused the sons of Israel, through the counsel of Balaam, to trespass against the LORD in the matter of Peor, so the plague was among the congregation of the LORD. Now therefore, kill every male among the little ones, and kill every woman who has known man intimately.’” (Numbers 31:15–17 NASB)
God killed 24,000 Israelite men for their debauchery with these women. These women were willing tools in Midian and Moab’s efforts to seduce the people of Israel into idolatry. This is why they were killed.
The men of Midian used their wives to seduce the Israelites. In the dual Torah readings מטות Matot and מסעי Massei, we learn that Midian did this under the belief that they had to conquer Israel for their own long-term survival, for the sake of their sons. Hence when God tells Israel to wipe out the men, wives and young boys, he is telling them to wipe out the people complicit in Israel’s idolatry and also the reason for which they excused their idolatry.