Where the story is told
Suhayb ar-Rumi, may Allah be pleased with him, reported that the Prophet ﷺ told of a king among the people before us. The report is in Sahih Muslim 3005. Another narration with some differences in detail is in Jami at-Tirmidhi 3340, which al-Tirmidhi called hasan gharib.
The Quran mentions the companions of the ditch in Surah al-Buruj (verses 4-8) without telling the details of their story. Below we follow the narration of Muslim step by step.