It was narrated from Ibn Mas’ud that the Messenger of Allah ﷺ said, “I used to forbid you to visit the graves, but now visit them, for they will draw your attention away from this world and remind you of the Hereafter.”
أَنَّ رَسُولَ اللَّهِ ﷺ قَالَ كُنْتُ نَهَيْتُكُمْ عَنْ زِيَارَةِ الْقُبُورِ فَزُورُوا الْقُبُورَ فَإِنَّهَا تُزَهِّدُ فِي الدُّنْيَا وَتُذَكِّرُ الآخِرَةَ