What precisely does the demise of righteous people atone for, and who gets the atonement?
Background:
In Parshat Chukath - במדבר פרק-כ - we learn from Rashi that the demise of the prophetess Miriam was juxtaposed to the chapter of the Red Heifer to teach us that both of these atone.
The Pasuk says:
וַתָּמָת שָׁם מִרְיָם וַתִּקָּבֵר שָׁם
Rashi:
ותמת שם מרים. למה נסמכה מיתת מרים (ל) לפרשת פרה אדומה, לומר לך, מה קרבנות (מ) מכפרין, (ס"א כמו שפרה אדומה מכפרת) אף מיתת צדיקים (נ) מכפרת (מועד קטן כח.):
Why was Miriam's death juxtaposed to the Red Heifer? To teach us that just as sacrifices atone (alternate reading: just as the Red Heifer atones) so too does the demise of the righteous atone.
Rashi is quoting the Gemara in Mo'ed Katan 28a - which has the alternate reading's text.
Tosafos there explains - מה פרה אדומה מכפרת פירוש על מעשה העגל וכדאמרינן במדרש משל לבן השפחה שטינף פלטירין של מלך - that the Red Heifer atones for the sin of the Golden Calf, as per the Medrash that the maidservant (Heifer) is expected to clean up her child's (Calf's) mess in the palace.
The שפתי חכמים elaborates:
ל) פירוש דאין כאן מקומו שהרי בשנה ראשונה ליציאת מצרים עשו העגל ובשנה שנייה שרפו פרה אדומה ומיתת מרים היתה בסוף מ' שנה ליציאת מצרים:
That is: The first RedHeifer was done soon after the Mishkan was erected - it should be in the Chumash around Tazria-Metzora. Miriam's demise happened 38 years later; the Red Heifer is put here - some 38 years after it happened - to teach us something.
He continues:
מ) ואם תאמר וכי קרבן היתה הפרה אדומה והלא לא היו מקריבין ממנה כלום. ויש לומר לפי דבר אחר שפירש"י לעיל שקראה הכתוב חטאת לומר שהיא כקדשים ליאסר בהנאה שמע מינה שהיא כקרבן:
That is: Even though the Red Heifer was not a real sacrifice, (it was done outside the Temple and no part of it was brought on the altar), Rashi already explained that it has some of the laws of a sacrifice, as it's called a חַטָּאת (ibid 19:9) ולפי הלכותיו קראה הכתוב חטאת, לומר שהיא כקדשים להאסר בהנאה - in that one is forbidden to derive personal benefit from it, [besides for its stated purpose. (As per שפתי חכמים there- ויש מפרשים כקדשים שבאין לכפרה כך האפר בא לטהר מי שצריך ואין שום הנאה אחרת ממנה).]
He concludes:
נ) ואם תאמר לפי זה היה לו לסמוך גבי קרבנות ממש. ויש לומר לכך נסמכה לפרה לפי שהם דומים להדדי לפי שמיתת צדיקים אינן קרבנות וגם הפרה אינה קרבן ממש ולמדנו מהדדי כמו שזה מכפר גם זה מכפר:
That is: Miriam's demise was juxtaposed to the Red Heifer, and not to a regular sacrifice, to highlight that neither are real sacrifices, yet they both atone.
Question:
What precisely does the demise of righteous people atone for, and who gets the atonement?
I'm asking, as the concept the death of the righteous atones is often quoted at funerals, implying that it's a Korban Tzibur - a general atonement for the public.
Sources and novel ideas are welcome.