It is narrated from Sayyiduna Abdullah bin Mas'ud (may Allah be pleased with him) that he said: "Among the nations before you there was a man who lived among a disbelieving people, and near them was a town of righteous people. The man said to himself: 'I have remained in disbelief for a long time; now I will surely go to this righteous town so that I may live as one of them.' So he set out, but while he was still on the way, death overtook him. Then (in the matter of taking his soul) the angel and Satan began to dispute; one said: 'I have the greater right to him,' and the other said: 'I have the greater right to him.' Then Allah, the Blessed and Exalted, sent them an angel from among His hosts (to decide), who said: 'Measure the distance between the two towns; whichever town this man is found closer to, he will be counted among its people.' So they measured the distance between the two towns and found him closer to the town of the righteous, so he was counted among those righteous people."
This chain is sahih (authentic).
There is a supporting narration (shahid) for it in the two Sahihs.
Hadith Referenceالمطالب العالية بزوائد المسانيد الثمانية / كتاب أحاديث الأنبياء عليهم الصلاة والسلام / 3466
Hadith Gradingمحدثین:إسناده صحيح
Hadith Takhrij«حکم الحدیث المطالب العالیہ محققاً تنسیق د. سعد بن ناصر بن عبد العزيز الشَّثري- مکتبہ شاملہ :قال ابن حجر: هَذَا إِسْنَادٌ صَحِيحٌ . » «أخرجه مصنف عبد الرزاق برقم: 20550، وأخرجه مصنف ابن أبي شيبة برقم: 35360، وأخرجه المعجم الكبير للطبراني برقم: 8851»