THE DEATH OF TZADIKIM ATONES
ויקרא פרק טז, א וַיְדַבֵּר ה' אֶל-משֶה אַחֲרֵּי מוֹת שְנֵּי בְנֵּי אַהֲרֹן בְקָרְ בָתָם לִפְנֵּי-ה' וַיָמֻתוּ :
ירושלמי מסכת יומא דף ז/ב פרק א הלכה ה
למה הוא מזכיר מיתתן ביום הכפורים, ללמדך שכשם שיום הכפורים מכפר על ישראל כך מיתתן שך צדיקים מכפרת על ישראל
The Gemara does not explain the reason that the death of Tzadikim atones. Apparently this is understood according to the Medrosh of Pirkei d’Rabbi Eliezer regarding the death of Shaul, “and he buried the bones of Shaul...and after that G-d entreated for the land.” Since G-d saw that Dovid did a chesed (he wept and eulogized Shaul), immediately G-d was filled with mercy.
Thus, it is not the death of Tzadkim itself that atones, but the honor and mourning that is accorded to the Tzadik (which is really honoring HaShem) that leads to atonement.