Where the story is told
This story is not in the Quran. It is an authentic hadith that the Prophet ﷺ told about people from earlier times. It is narrated by Abdullah ibn Umar and is found in Sahih al-Bukhari (2272, 3465 and 5974, with a similar version in 2215 and 2333) and in Sahih Muslim 2743.
The wording differs a little between the narrations, for example in the order of the three supplications and some small details. The meaning is the same. Here we follow the order found in Sahih al-Bukhari 5974 and Sahih Muslim 2743.