The sex starts with Balaam. Yeah - the guy with the talking donkey.
He tried to get a curse from God to pronounce over the people of Israel. The Bible dedicates an entire chapter to the endeavor. Three prophecies come out, each one more strongly in favor of the people of Israel than the one before.
So he comes up with an alternate plan. Send out the beautiful, lucious women.
No kidding! And it works.
The men go "gaga", start having sex with them, and start worshipping their idols, which are part of a blood thirsty and sex oriented religion. "While Israel was camped at Shittim (Acacia Grove), the men began to have sex with the Moabite women. It started when the women invited the men to their sex-and-religion worship. They ate together and then worshiped their gods. Israel ended up joining in the worship of the Baal of Peor." (Numbers 25:1-3)
How does God feel about this? Not happy.
Triple disobedience. 1. Don't mingle with the locals. 2. Don't have sex outside of marriage. 3. Don't worship any gods other than God almighty. As a culmination of haughty disregard for God's standards, one of the leaders proudly parades his beauty in front of everybody, takes her into his tent in broad daylight, and starts "doin what comes nachrally". The priest of God went into the tent and put a sword through the both of them. Put out the passion rather suddenly...
(Its all in there. Chapter 25)
God was S-O-O-O angry, he started a plague among the people. The action of this priest stopped the plague, but 24,000 people died. (Did I say God was angry?) "God spoke to Moses: "Phinehas son of Eleazar, son of Aaron the priest, has stopped my anger against the People of Israel. Because he was as zealous for my honor as I myself am, I didn't kill all the People of Israel in my zeal. So tell him that I am making a Covenant-of-Peace with him." (Leviticus 25:10-13)
Plop and Roll
So it is some time later.
"God spoke to Moses: "Avenge the People of Israel on the Midianites. Afterward you will go to be with your dead ancestors." (Numbers 31:1,2) So Moses sends only 12,ooo men to deal with the Midianites. If I understand correctly, they killed EVERY Midianite man, and not a single Israelite soldier died. Not quite sure how that happened, but it appears that it did.
What happened to Balaam? He was killed too...
So the Midianites plopped down dead and rolled over. (I know, it is a sad attempt to be "cute", and it didn't work very well.)
Several family groups negotiate to settle into the land and houses they have just emptied. The conquest and take over of the promised land has begun!