"G-d spoke to Moses, saying: Take revenge for the Israelites against the Midianites. Then you shall die and be gathered to your people" (31:1-2).
How ironic that Moshe's death is linked to the defeat of the Midianites. Many years earlier, it was the people of Midian that saved Moshe's life. "When Pharaoh heard this (about Moshe's murder of the Egyptian), he sought to kill Moshe, but he ran from Pharaoh and dwelled in the land of Midian " ( Shemot 2:15).
Not only did Midian prove a refuge for Moshe, it provided him with a wife and family. No doubt, Moshe would have remained in Midian for his entire life had G-d not literally forced him to go and redeem the Jewish people. Yet despite their role in the saving and nurturing of Moshe, and the close family ties, justice demanded that Midian--whose name is related to the word din , strict justice--be attacked.
For although it was Balak, the king of Moav, who led the efforts to stop the march of the Jewish people into Israel, it was the daughters of Midian who engaged in sexual immorality with B'nei Yisrael , unleashing a plague and causing the deaths of thousands. Furthermore, as Rashi notes, one can understand why Moav, a country along the route that the Israelites took as they marched toward Israel , feared an invasion. But what was Midian's excuse for interfering with the Jews?
The relationship between Midian and the Jewish people goes back to long before their tragic encounter in year 40 in the desert. "Then there passed by Midianite merchantmen; and they drew and lifted Joseph out of the pit, and sold Joseph to the Ishmaelites for twenty pieces of silver" ( Breisheet 37:28). Our sojourn in Egypt began when the Midianites sold Joseph to slavery in Egypt (the debate regarding the actual role of the brothers in the sale need not concern us now), and our entry into the land of Israel could begin only after Midian was defeated.
"And Moshe took the bones of Joseph with him" ( Shemot 15:4). Building a nation requires remembering our past and paying proper homage to those upon whose shoulders we rest. It was Joseph who enabled the continuation of Jewish history, laying the foundation for a nation to develop in a foreign land. It was Moshe, building on that foundation, who gave a purpose and goal to that history. While the nation was busy fleeing Egypt , Moshe was busy honoring a promise made before he was even born.
Midian used sexual immorality as their weapon against the Jewish people, and almost succeeded in their attempts to have us destroyed. They knew the G-d of Israel abhors sexual misconduct. Perhaps it was in the merit of Joseph that they ultimately failed. Joseph is known as Yoseph Hatzadik --the righteous one--due to his withstanding of the sexual advances of Potiphar's wife. The Midrash claims that, at that moment, Joseph stood at the threshold of Jewish history, and that had he succumbed, he would have been rejected from the Jewish nation, like so many of his relatives. Apparently, the survival of the Jewish people is intertwined with our sexual mores, with the defeat of Midian.
The command to defeat Midian is, in essence, a command enabling the Jewish people to establish "a kingdom of priests and a holy nation" ( Shemot 19:6)--our tradition equating holiness with sexual morality (see Rashi, Vayikra 19:1).
Moshe, eager to lead the nation towards holiness, immediately prepared for battle. His own death in the process was a small price to pay. The battle was won, and Moshe's job was thus complete. It was time for him to be "gathered" to his people in peace and tranquility, knowing that the promise made to Abraham, the actual father of Midian ( Breisheet 25:2), to be the founder of a great nation in the land of Israel , was soon to be fulfilled.