Another interpretation of (Numbers 3:40) "Number every firstborn male." The Holy One, blessed be He, said: For the sake of My love for Israel I altered the order of the world. How so? I wrote in My Torah that a donkey should be redeemed with a lamb, as it is said (Exodus 34:20), "And the firstling of a donkey you shall redeem with a lamb." Yet I did not do so; rather, I redeemed a lamb with a donkey. The Egyptians are likened to a donkey, as it is said (Ezekiel 23:20), "whose flesh is the flesh of donkeys"; and Israel are called a lamb, as it is said (Jeremiah 50:17), "Israel is a scattered sheep." And I slew the firstborn of the Egyptians, and I sanctified the firstborn of Israel, as it is said (Numbers 8:17), "For every firstborn among the children of Israel is Mine, of man and of beast; on the day that I struck down every firstborn in the land of Egypt I sanctified them for Myself." Therefore He said to him, "Number every firstborn male among the children of Israel."
Why God Redeemed Israel the Lamb with Egypt the Donkey
Midrash Tanchuma Buber, Bamidbar 24:1
ד"א פקוד כל בכור זכר. אמר הקב"ה בשביל חיבתם של ישראל שניתי סדרו של עולם, כיצד כתבתי בתורתי שיהא חמור נפדה בשה, שנאמר ופטר חמור תפדה בשה (שמות לד כ), ואני לא עשיתי כן, אלא פדיתי שה בחמור, המצריים נמשלו בחמור, שנאמר אשר בשר חמורים בשרם (יחזקאל כג כ), וישראל נקראו שה, שנאמר זה פזורה ישראל (ירמיה נ יז), והרגתי את בכוריהם של מצריים, והקדשתי בכוריהן של ישראל, שנאמר כי לי כל בכור [בבני ישראל באדם ובבהמה ביום הכותי כל בכור בארץ מצרים הקדשתי אותם לי (במדבר ח יז), לכך אמר לו פקוד כל בכור זכר בבני ישראל].