Abu Musa Ashari reported,

The Messenger of Allah ﷺ said, "When Allah intends mercy for a nation among His servants, He takes the soul of their prophet before them, making him a forerunner and predecessor for them. But when He intends the destruction of a nation, He punishes them while their prophet is still alive, and He comforts the prophet's eye by their destruction, since they had rejected him and disobeyed his command."

Azami 682

Sahih

عن أبي موسى قال

قال رسول الله ﷺ: إن الله إذا أراد رحمة أمة من عباده قبض نبيها قبلها، فجعله لها فرطا وسلفا، وإذا أراد هلكة أمة عذبها، ونبيها حي، فأقر عينه بهلكتها حين كذبوه، وعصوا أمره.

الأعظمي 682صحيح
Contents
Reference
Commentary
Ayat
Collection
Jami al-Kamil
Book
Faith
Chapter
Belief in the messengers, peace and blessings be upon them
Section
When Allah intends mercy for a nation, He takes its prophet before it
Numbering
Jami al-Kamil 682 (2nd edition)
Azami 682