Good question. Here are two explanations that I found interesting. Will leave it to others who are more scholarly to say how viable either might be.
CONTRADICTIONS IN THE BIBLE
Identified verse by verse and explained using the most up-to-date
scholarly information about the Bible, its texts, and the men who
wrote them — by Dr. Steven DiMattei
*#297. Who led the Israelites to worship Baal of Peor: the Moabites OR the Midianites? (Num 25:1-2 vs Num 25:16-18, 31:15-16)
*#298. What was their punishment: they were impaled OR killed by plague (Num 25:4-5 vs Num 25:8, 25:18)**
As noted previously (#287), the Balaam pericope (Num 22-24) and the Baal Peor apostasy (Num 25) both present a viable Moabite force in
northern Moab, contradictory to the traditions preserved in the
Yahwist which spoke of an Amorite presence and Amorite territory. See
also #282-285.
Similarly, the present story of the Israelites’ apostasy and
attachment to the cult of Baal at Peor clashes pretty heavily with the
rather positive presentation of the Israelites as loyal to Yahweh
throughout Numbers 21-24, on account of which Yahweh had granted them
a victory against the Canaanites at Hormah (but see #271-273), a
victory against the Amorites in northern Moab (#282-285), a victory
against the desired cursing of the Israelites by the Moabite king
Balak, and future victories against Moab and Edom, and pronouncements
against the Amalekites, the Kenites, and the Assyrians, but not
Israel, in Balaam’s final oracle.
Theologically, then, Numbers 21-24 display no divine knowledge of the
impeding apostasy here represented in Numbers 25, nor for that matter
does Numbers 25 display any knowledge of the Balaam incident, nor the
fact that this territory was Amorite, not Moabite according to the
earlier J tradition. In other words, the present story, like the
Balaam pericope, comes from a different textual tradition and was for
whatever reason placed in its present position by later editors.
Additionally, there seems to be two traditions here: an earlier
version where the Moabites, specifically Moabite woman, lead the
Israelites into the cult of Baal, and a later reworking where the
Midianites are added into the mix. For instance, the primary account
of the Baal Peor incident, Numbers 25:1-5, records that the affair
only involves Israelites and Moabites, But in Numbers 25:16-17 and
31:15-16 it now becomes the Midianites, exclusively, who have led the
Israelites into apostasy. Furthermore, in connection with the Moabite
woman of Numbers 25:1-5, the Israelites who become associated with
Baal Peor “are impaled to Yahweh” facing the sun. But in reference to
the Midianite storyline, Yahweh sends a plague (Num 25:8-9, 18). And
while in the Midianite storyline, their role in leading Israel to
apostasy is given as the pretext to exterminate all Midianites save
virgin girls (Num 31:1-20), nothing is said about the Moabites!
Biblical scholars have long recognized on both thematic and linguistic grounds that the story starting at Numbers 25:6, as well as
all of chapter 31, belongs to the Priestly source, and that it was the
Aaronid priests themselves who added the Midianites into this story to
serve their own ideology.
This should not come as a surprise. We have already seen in numerous
other Priestly reworkings of earlier JE material the degradation of
the Midianites, and/or the omission of both Moses’ and Yahweh’s
association with Midian. So, by way of review:
• In P’s retelling of the Sinai revelation, Yahweh reveals himself to
Moses while he is still in Egypt, thus not in Midian (#87). In fact,
in P’s retelling there is no Midianite sojourn; Moses never goes to
Midian in P!
• P ignores the tradition that Moses’ wife was a Midianite. Indeed, in
the present context one wonders if Moses’ wife is among those
Midianite woman commanded to be slain: “kill every woman who has known
a man” (Num 31:17)
• P (as well as D) expunges from the historical record any mention of
the Midianite Jethro, Moses’ father-in-law, as the first priest to
officiate sacrifices to Yahweh, while Aaron stood by (Ex 18:12)—an
Elohist text!
• And now here in the retelling of the Baal Peor incident, P exchanges
the guilty Moabite woman for Midianite woman, and thus mandates, using
this as a pretext, the complete exterminate of all Midianites!
In light of these rewritings, and P’s subtle slighting of Moses at
times (e.g., #93, #95, #105), scholars surmise that the Aaronid
priesthood must have been uncomfortable with Moses’ Midianite
connections, and what that implied—i.e., that Yahwism came from
Midian! So what did the Priestly writers do? They rewrote the
tradition—nay, they rewrote “history.” P completely omits Moses’
Midianite sojourn; ignores the tradition that Moses’ wife was
Midianite; eliminates the Midianite priest of Yahweh, Jethro; changes
the Moabite foes into Midianite foes in the story now preserved in
Numbers 25:6-19; and mandates the wholesale slaughter of Midian in
Numbers 25:16-18 & 31:1-20.
We might speculate that the Aaronid Priestly writer was attempting to
purge Yahweh from any connections he may have had in the earlier
sources to the Canaanite El or a Midianite deity or origin. What P’s
El Shaddai covenant passages effectively do is claim that the deity
whom the earliest Israelites-Canaanites worshiped when they built
altars for El was in fact Yahweh as El Shaddai (see #11, #27,
#294-296)! Likewise purging Yahweh from any sort of Midianite connection was also a way of presenting Yahweh as more Israelite in
character and origin. I’ll end this with an excerpt from William Propp
(Exodus 19-40, 750):
Because of Moses’ familial relationship with Jethro the priest of
Midian [Yahweh’s priest!], because of Midian’s proximity to mount
Sinai, because Yahweh is said to come from the south both in the Bible
(Deut 33:2; Judg 5:4-5; Ps 68:8-9, 17-18) and in an inscription from
Kuntillet ‘Ajrud, “Yahweh of Teman,” and because the Egyptians
encountered “Yahweh Shasu” [Egyptian texts from the reigns of
Amenophis III, Ramesses II, Ramesses III mention seminomads called the
“Shasu of yhw3” located in the vicinity of Midian]… a popular
scholarly theory is that Israel learned to worship Yahweh from Midian.
Although this view is favored, and even indorsed somewhat, by the
earlier Elohist tradition and older traditions now found in the Psalms
and elsewhere in the Bible, the Priestly writer sought to expunge this
tradition from the historical record! Little did he know however that
the new “history” he wrote to replace these older traditions would one
day be assembled together with these older traditions and even
centuries later labeled as “the Book” by readers who knew nothing of
the Priestly writer’s agendas, concerns, worldviews, and beliefs!