"And the LORD said to Moses: Speak to Aaron your brother" (Leviticus 16:2). For I have already told you concerning him, "Is not Aaron the Levite your brother" (Exodus 4:14). And say to him: "that he come not at all times [into the holy place]"; and if he transgresses the command, he shall die as his sons died. "Your brother" — he is under the prohibition "that he come not," but you are not under the prohibition "that he come not." "At all times" — this refers to the Day of Atonement. "Within the veil" — this refers to the innermost place. "That he die not" — this is a punishment. "For in the cloud I will appear upon the kapporet" — this is a warning. And why was the death of the two sons of Aaron placed adjacent to the section of the Day of Atonement? To teach you that the death of the righteous atones like the Day of Atonement.
Why Aaron's Sons Died Beside the Yom Kippur Law in Midrash Aggadah
Midrash Aggadah, Leviticus 16:2
ויאמר ה' אל משה דבר אל אהרן אחיך. שאמרתי לך עליו הלא אהרן אחיך וגו' (שמות ד', יד), ואמור לו ואל יבוא בכל עת [אל הקודש] ואם יעבור הצווי ימות כאשר מתו בניו: אחיך. בבל יבוא ואין אתה בבל יבוא: בכל עת. זה יום הכיפורים: מבית לפרוכת. זה לפני לפנים: ולא ימות. הרי זה עונש: כי בענן אראה על הכפורת. הרי זה אזהרה. ולמה נסמכה מיתת שני בני אהרן לפרשת יום הכיפורים, ללמדך שמיתת הצדיקים מכפרת כיום הכיפורים: