[Another interpretation of (Numbers 3:40:) "Enroll every firstborn male."] But behold, I bring them forth—the two hundred and seventy-three firstborn who were found to be in excess over the Levites at the time when Moses counted them. R. Judah and R. Nehemiah differ. R. Judah says: This is what Moses did. He took {two hundred and seventy-three} [twenty-two thousand] tickets, and wrote upon them "Levi, Levi." And he took a further two hundred and seventy-three tickets, and wrote upon them "five sela'im." He shuffled them and placed them in an urn. And the father of a firstborn would put forth his hand into the urn. If there came up in his hand a ticket on which was written "son of Levi," he was redeemed, and was exempt from the five sela'im. But if there came up in his hand a ticket on which was written "five sela'im," he would give five sela'im. These are the words of R. Judah. R. Nehemiah says: There is still a dispute in the matter, for he could say to him, "There is no ticket here on which is written 'son of Levi'; therefore it did not come up in my hand." Rather, this is what Moses did. He took tickets according to the number of all the firstborn, and wrote upon them "son of Levi." And he took yet other tickets according to their number, and wrote upon them "five sela'im." He shuffled them and placed them in an urn. And the father of a firstborn would put forth his hand into the urn. If there came up in his hand a ticket of "son of Levi," he knew that a son of Levi had redeemed him, and he exempted him from the five sela'im. But if there came up in his hand a ticket of "five sela'im," [he would give five sela'im]. And the appointed officer said to him, "Is there not there another ticket on which is written 'son of Levi'? And it was not fitting to be redeemed by a son of Levi."
How Moses Redeemed the 273 Surplus Firstborn by Lot
Midrash Tanchuma Buber, Bamidbar 25:1
[ד"א פקוד כל בכור זכר]. והרי אני מוציאם מאתים ושבעים ושלשה בכורות שנמצאו עודפים על הלוים בשעה שמנה אותם משה. ר' יהודה ור' נחמיה. ר' יהודה אומר כך עשה משה, נטל (מאתים ושבעים ושלה) [שנים ועשרים אלף] פיטקים, וכתב בהם לוי לוי, ונטל עוד רע"ג פיטקים, וכתב בהם חמש סלעים, טרפן ונתנן בקלפי, והיה אביו של בכור מושיט ידו בקלפי, אם עלה בידו פטק שכתיב בו בן לוי היה נפדה, והיה פטור מה' סלעים, ואם עלה בידו פטק שכתוב בו ה' סלעים, היה נותן ה' סלעים, אלו דברי ר' יהודה, ר' נחמיה אומר עדיין היא מחלוקת בדבר, שהיה יכול לומר לו אין כאן פטק שכתוב בו בן לוי, לפיכך לא עלה בידי, אלא כך עשה משה, נטל פטקין כמנין כל הבכורות, וכתב בהן בן לוי, ועוד לקח פתקים אחרים במניינם, וכתב בהן חמשה סלעים, טרפן ונתנן בקלפי, והיה אביו של בכור מושיט ידו בקלפי, אם עלה בידו פטק של בן לוי, היה יודע שפדאו בן לוי, והיה פוטרו מה' סלעים, ואם עלה בידו פטק של ה' סלעים, [היה נותן חמשה סלעים], והממונה אמר לו הלא יש שם פטק אחר שכתוב בו בן לוי, ולא היתה ראוי לפדות בן לוי.