The story proceeds from that of the prophetBalaam, in which he ascends the mountain of Pe‘or, and makes sacrifices to God from atop it. Having finished his sacrifices, Balaam views the Israelites on the plain below, and although hired to curse them, pronounces a blessing over them, prophesying their blessed nature and destruction of Moab and the other adversaries of Israel. The Israelites, after spending a short time in the plain of Moab, begin to involve themselves with the Moabite women. Consequently, under the influence of Moabite culture, the Israelites begin participating in the worship of the Moabite gods, and join themselves to Baal Peor, in the Septuagint Beelphegōr, a baal associated with Mount Pe‘or. Yahweh orders Moses to gather the chiefs of the people and hang up the idolaters before Yahweh to turn away Yahweh's anger. The scene then abruptly shifts from concerns about Moabites to those about Midianites. A man — Israelite Zimri, the son of Salu — brings a Midianite woman Cozbi into the camp in the sight of Moses, where the people are weeping. Phinehas, grandson of Aaron, thereupon rises up with a spear, follows the man into a tent and thrusts the spear through both the man and woman. The plague, from which 24,000 had died, then stopped. Because of his violent action, God then promises Phinehas a "covenant of peace" and a "covenant of eternal priesthood". God then commands Moses to wage a war of revenge against the Midianites. However, the war is only launched several chapters later, in.
The abrupt change from concerns about the Moabites to the Midianites, and mention of the end of a plague which is never described as starting, is explained by the documentary hypothesis as being due to a change from the JE source to the Priestly source. Despite the Torah redactor usually, according to the hypothesis, interlacing together the sources, the stories here were evidently difficult to combine, as the redactor appears to have cut the end from the JE version, and the beginning from the Priestly version, and joined the resulting texts together. As the Priestly source is considered to be based on the JE source, albeit spun to the Aaronid priesthood's political viewpoint, the original JE ending, and priestly source beginning, can be expected to be fairly similar to the text that is currently present. Nevertheless, the use of plagues, by God, to punish the people in very large numbers, is unique to the Priestly source, once the Israelites have left Egypt, and consequently this detail is unlikely to have been present in the original. While it is considered uncertain as to why the Priestly source would change Moab to Midian, it is generally agreed amongst critical scholars that the account of the war against Midian, and its spoils, originates from a writer who added these elements to the original version of the Priestly source. The command to vex the Midianites is also considered a part of these additions, and thus the revenge on Midian serves as a vehicle for this writer to include the list of spoils, and its distribution, which has a larger amount of text than the narrative of the war. Thus, this later writer could easily have changed Moab to Midian to facilitate this addition.