Ramban discusses the Levites' zeal during the sin of the Golden Calf and how that act helped establish their special status for the future. It is a key Rishonim source for how Levi's zeal was transformed into priestly service.
וְטַעַם וַיַּעֲמֹד מֹשֶׁה בְּשַׁעַר הַמַּחֲנֶה דָּבֵק בְּמָה שֶׁאָמַר, כִּי כֵּיוָן שֶׁרָאָה אֶת הָעָם שֶׁיִּהְיֶה לְשִׁמְצָה וּלְקַלָּסָה בְּאוֹיְבֵיהֶם, וְזֶה חִלּוּל הַשֵּׁם, עָמַד בְּשַׁעַר הַמַּחֲנֶה וְקָרָא בְּקוֹל גָּדוֹל מִי לַה' אֵלָי, וְהָרַג בְּפַרְהֶסְיָא כָּל עוֹבְדֵי הָעֵגֶל כְּדֵי שֶׁיִּשָּׁמַע הַדָּבָר בְּקָמֵיהֶם, וְיִתְקַדֵּשׁ שֵׁם שָׁמַיִם בָּהֶם תַּחַת הַחִלּוּל שֶׁעָשׂוּ: וְטַעַם כֹּה אָמַר ה' אֱלֹהֵי יִשְׂרָאֵל בַּעֲבוּר שֶׁחָשְׁבוּ עוֹבְדֵי הָעֵגֶל לִזְבּוֹחַ לֵאלֹהֵי יִשְׂרָאֵל, וְהִנֵּה מִדַּת הַדִּין פּוֹגַעַת בָּהֶן בַּעֲבוּר שֶׁקִּצְּצוּ בַּנְּטִיעוֹת. וְעוֹד, כִּי הַמִּשְׁפָּט לֵאלֹהִים הוּא (דברים א יז). וּמִפְּנֵי זֶה אָמַר בַּלְּוִיִּים "כִּי הִבְדִּיל אֱלֹהֵי יִשְׂרָאֵל אֶתְכֶם מֵעֲדַת יִשְׂרָאֵל" (במדבר טז ט), כִּי הָעֲבוֹדָה לֵאלֹהֵי יִשְׂרָאֵל, וְלִשְׁמוֹ נִבְדְּלוּ בַּזְּכוּת הַזֹּאת: שִׂימוּ אִישׁ חַרְבּוֹ עַל יְרֵכוֹ הָיוּ עוֹבְדֵי הָעֵגֶל רַבִּים וְלֹא יוּכְלוּ לַהֲבִיאָם לְב"ד, עַל כֵּן צִוָּה לְכָל בְּנֵי לֵוִי לַחְגֹּר חַרְבָּם, עַל דֶּרֶךְ מַה שֶׁאָמְרוּ רַבּוֹתֵינוּ (סנהדרין מה:) שֶׁאִם אֵין אַתָּה יָכוֹל לַהֲמִיתוֹ בְּמִיתָה הָאֲמוּרָה בּוֹ אַתָּה יָכוֹל לַהֲמִיתוֹ בְּכָל מִיתָה שֶׁתּוּכַל. וְהִנֵּה זֶה הוֹרָאַת שָׁעָה לְקַדֵּשׁ הַשֵּׁם, שֶׁלֹּא הָיְתָה בָּהֶם הַתְרָאָה, כִּי מִי הִתְרָה בָּהֶם, אֲבָל הָיוּ בְּנֵי לֵוִי מַכִּירִים בְּאֵלּוּ הַנֶּהֱרָגִים שֶׁהֵם הָיוּ עוֹבְדָיו: וְעַל דַּעַת הָאוֹמֵר (יומא סו:) עֵדִים וְהַתְרָאָה בִּידֵי אָדָם, כְּמוֹ שֶׁכָּתַב רַשִׁ"י (רש"י על שמות ל"ב:כ'), נֹאמַר שֶׁצִּוָּה אוֹתָם לַחְגֹּר חַרְבָּם לִתְפֹּשׂ אוֹתָם וְלַהֲבִיאָם לִפְנֵי מֹשֶׁה אוֹ סַנְהֶדְרִין, וַאֲשֶׁר מְצָאוּהוּ בְּעֵדִים וְהַתְרָאָה הֲרָגוּהוּ בִּסְקִילָה כְּמִשְׁפַּט עוֹבְדֵי ע"ז, אוֹ שֶׁהָיְתָה מִיתָתָם בְּסַיִף כְּעִיר הַנִּדַּחַת. וְהִנֵּה כָּל בְּנֵי לֵוִי הָיוּ לַה' וּמַתְרִים בָּעָם שֶׁלֹּא יִשְׁתַּחֲווּ לוֹ וְלֹא יִזְבְּחוּ לוֹ, כִּי אַהֲרֹן לֹא צִוָּה לָחֹג בִּלְתִּי לַה' לְבַדּוֹ כַּאֲשֶׁר פֵּרַשְׁתִּי. וְהַנָּכוֹן בְּעֵינַי כְּדִבְרֵי הָאוֹמֵר (יומא שם) זִבַּח וְקִטֵּר בְּסַיִף, גִּפֵּף וְנִשֵּׁק בְּמִיתָה, שָׂמַח בְּלִבּוֹ בְּהִדְרוֹקָן, כִּדְאִיתָא בְּמַסֶּכֶת יוֹמָא. וְהִנֵּה הַכֹּל מַעֲשֵׂה שָׁעָה, כִּי אֵין מְגַפֵּף וּמְנַשֵּׁק בְּמִיתָה לְדוֹרוֹת (סנהדרין ס:):
AND MOSES STOOD IN THE GATE OF THE CAMP. This is connected with what is said in the preceding verse, thus stating that when Moses saw that the people would be a source of derision and disgrace amongst their enemies, and would thereby profane the Name of G-d, he stood in the gate of the camp and called out loudly, Whoso is on the Eternal’s side, let him come unto me; and they killed publicly all those who worshipped the calf, so that the matter be heard about amongst their enemies, and G-d’s Name would thus be sanctified, in place of the profanation that they had caused. THUS SAITH THE ETERNAL, THE G-D OF ISRAEL. The reason for this expression is that since the worshippers of the calf had intended to sacrifice to the G-d of Israel, therefore the attribute of justice extracted punishment from them, because they “mutilated the shoots” [of faith], and besides, for the judgment is G-d’s. It is for this reason that he [Moses], said of the Levites, for the G-d of Israel hath separated you from the congregation of Israel, for the Service is to the G-d of Israel, and to His Name they were separated by virtue of this meritorious deed. PUT YE EVERY MAN HIS SWORD UPON HIS THIGH. Since there were many worshippers of the calf, and they could not have all been brought to the court, therefore Moses commanded all the sons of Levi to put on their swords, in a similar way to that which our Rabbis have said, that if you cannot administer to the guilty the specific kind of death mentioned for his case, you may execute him by any means that you can. Now this procedure was a decision only for an emergency, in order to sanctify G-d’s Name, since those who worshipped [the calf] had not been forewarned [of the death penalty], for who had warned them beforehand? The sons of Levi, however, recognized those whom they killed as the worshippers of the calf. And in the opinion of the Sage who says that if there were witnesses [to the act of idolatry] and forewarning of the penalty, the offender’s death was by hand of man, as Rashi wrote, then we shall interpret the meaning of the verse to be that Moses commanded the sons of Levi to put on their swords and take hold of the offenders by force and bring them to court before Moses or before the Sanhedrin. Those who were found to have worshipped the calf in the presence of witnesses and with forewarning, were then put to death by stoning, as is the punishment of those who worship idols, or perhaps their death was by the sword, as is the punishment of a whole city gone astray. Thus all the sons of Levi remained loyal to G-d, and it was they who had warned the people not to worship the calf or sacrifice to it, seeing that Aaron had only commanded to hold a feast to the Eternal alone, as I have explained. The correct interpretation is in accordance with the opinion of the Sage who says: “Those who slaughtered or burnt the sacrificial portions [to the calf] were punished by the sword; those who embraced and kissed it, were punished with death [by the hand of Heaven]; those who merely rejoiced in their heart, were afflicted with dropsy,” as is stated in Tractate Yoma. Thus it was all a decision under circumstances of emergency, because embracing or kissing an idol does not make one liable to death [by the hand of Heaven] in all future generations.