عَنْ أَنَسِ بْنِ مَالِكٍ رَضِيَ اللَّهُ عَنْهُ قَالَ: قَالَ رَسُولُ اللَّهِ صَلَّى اللَّهُ عَلَيْهِ وَآلِهِ وَسَلَّمَ: ((نَهَيْتُكُمْ عَنْ زِيَارَةِ الْقُبُورِ، ثُمَّ بَدَا لِي أَنَّهَا تُرِقُّ الْقَلْبَ وَتُدْمِعُ الْعَيْنَ وَتُذَكِّرُ الْآخِرَةَ فَزُورُوهَا وَلَا تَقُولُوا هُجْرًا))
It is narrated from Sayyiduna Anas bin Malik (may Allah be pleased with him) that the Messenger of Allah (peace and blessings be upon him) said: I had forbidden you from visiting graves, but then I realized that visiting them softens the hearts, brings tears to the eyes, and reminds one of the Hereafter. Therefore, visit the graves, but do not commit any indecent acts.