The Gemara (Perek Hanisrafin, Sanhedrin 82a), in its discussion of the Mishna (81b), teaches that one who has relations with a heathen woman may be killed by a zealot, noting that R. Dimi states that the beth din of the Hasmoneans decreed that one who has relations with a heathen woman is in violation of [four forbidden actions, for which the Gemara gives a mnemonic] NaSHGA – nun, shin, gimmel, aleph. Rabin states [with a slight variation] that in such a case one is in violation of NaSHGaZ – nun, shin, gimmel, zayyin. Both agree about the first three violations: Nun – niddah, a menstruant woman; shin – shifcha, a heathen slave woman; gimmel – goyya, a heathen woman. They differ regarding the fourth. R. Dimi is of the opinion that in such a situation one is in violation of having relations with a married woman – aleph, eshet ish. Rabin, on the other hand, does not view this possibility because heathens do not recognize the marriage bond. Thus his understanding of the Hasmonean edict was that the final violation, zayyin, refers to zonah – having relations with a harlot (if the man was a kohen). But R. Dimi assumed that heathen men, too, do not permit their wives to be promiscuous, and their relationship is likened therefore to ishut’ marriage.
R. Chisda stated that if one comes to ask the beth din whether to punish the transgressors by
killing them, the judges do not instruct him to do so, since the Mishna states that “the zealot may kill him.” This is a self-inspired, natural reaction. The Gemara notes a similar view of Rabbah b. Bar Hana, who said in the name of R. Yochanan that one who inquires of the beth din is not advised in this matter. Moreover, had Zimri left the woman and Pinchas had killed him, Pinchas would have been liable for the death penalty on this account. Furthermore, had Zimri in the midst of this act killed Pinchas, he would not have been liable for the death penalty because Pinchas would be considered to be his pursuer, from whom he is entitled to save himself.
The Gemara elaborates further regarding the events that led to the incident of Zimri and Cozbi:
The verse states in Parashat Balak (Numbers 25:5), “Vayomer Moshe el shoftei Yisrael, Hirgu ish anashav hanitzmadim le[B]aal-peor – Moses said to the judges of Israel, Let each man kill his men who were attracted to Baal-peor.”
When the tribe of Shimon saw that members of its tribe were to be punished with execution for their sin of idolatry, they went to their prince, Zimri ben Salu, and said: “They are judging cases of capital punishment [against our tribe] and you sit by silently!” Zimri then arose and gathered 24,000 people from among Israel and went to Cozbi, the daughter of the Midianite king. He asked Cozbi to submit to him, to which Cozbi replied, “I am the king’s daughter, and my father has commanded me not to submit except to the greatest of them [namely, Moses].” Zimri replied that he was a tribal prince, and his tribe was greater than Moses’ tribe, for Zimri’s tribe was descended from the second of Jacob’s sons, while Moses’ tribe was descended from the third-born son (Levi).
Zimri then grabbed her by her hair and brought her before Moses, explaining: “Son of Amram, is this one forbidden or permitted [to me]” And if you say she is forbidden, who permitted the daughter of Jethro [also a Midianite woman] to you?”
At that point Moses momentarily forgot the halacha that a zealot may kill one who has relations with a heathen woman.
The people began to weep loudly, as it is written, “And they were weeping at the entrance of
the Tent of Meeting.”
The Gemara continues: “Pinchas, the son of Eleazar, the son of Aaron the priest, saw. . .” The
Gemara asks, “What did he see?” He saw this incident and remembered the halacha, saying to
Moses, “Did you not teach us that one who has relations with a heathen woman may be killed?” Moses responded, “The one who reads the letter is the agent designated to carry out its instructions.”
Shmuel adds that Pinchas saw that “Ein chochma ve’ein tevuna ve’ein etzah lenegged Hashem
– There is no wisdom, nor understanding, nor counsel against [the honor of] Hashem.” [See
Proverbs 21:30, Rashi (ad loc. s.v. Shmuel amar…) and Tosafot (Gittin 68a s.v. U’chetiv zenut
ve’yayyin…), who explain the concept of attributing a verse that was not yet written.]
Whenever there is a situation of chillul Hashem, the desecration of Hashem’s holy name, we
do not worry about showing respect to a teacher. Therefore, though Pinchas issued a ruling in the presence of his master, Moses, he was not guilty of any wrongdoing in this case.
Thus it was Zimri’s bold act of violation that required Pinchas to punish him immediately,
whereas later on Pinchas would not be allowed such an impulsive act on his own. The sin of the army commanders was that they had let the Midianite women live and Pinchas was powerless to act on his own. Rather, the order to kill the Midianite women was part of the general war instructions, and that is why Moses correctly rebuked the army commanders. [Or HaChayyim and Kli Yakar give slightly different explanations about the reason the commanders did not follow the instructions.]
The Gaon R. Moshe Feinstein (quoted by R. Avraham Fishelis in Kol Ram, Parashat Mattot)
notes Rashi’s statement (Numbers 31:12) that the army commanders were righteous men and they were not suspected of taking spoils without permission. Instead, they brought them to Moses. R. Feinstein asks: What is the theft committed in the midst of war, when such matters are usually permitted to soldiers? He explains that in the course of war one may become lax in the matter of murder and theft, and as such they wished it would not affect their souls. Thus they brought the spoils to Moses along with the females who had remained alive. We understand that Moses explained to them that such is not the case. The ruling is that the females must be killed along with the males. When it comes to G-d’s war, we must wage the battle exactly as specified. (See also Noam Elimelech on Parashat Pinchas. He explains in detail the need for war against Midian and for that people’s destruction.)