Visiting the graves at Baqi al-Gharqad cemetery in Madinah is a sunnah Sunnah for men. Nabi (shallallahu alaihi wa sallam) used to visit Baqi frequently and would make doa for those buried there. Many of the greatest companions are buried at Baqi, including Uthman ibn Affan, the wives of Nabi (except Khadijah), and many others. Visiting their graves is an act of remembrance and a source of reflection.
Sheikh Ibn Baz stated that the visitor should greet the deceased with the Sunnah greeting: 'Peace be upon you, O people of the abode, from among the believers and the Muslims. May Allah have mercy on those who have gone ahead and those who will follow. We ask Allah for well-being for us and for you.' The visitor then makes doa for the deceased and reflects on death and the Hereafter. This is the purpose of visiting graves — remembrance of death and doa for the dead.
Sheikh Ibn Baz also warned against innovations that some people practice at graves: wiping or touching the graves, asking the deceased for intercession or help, placing flowers or cloth on the graves, sitting on the graves, praying toward the graves, or making the visit a formal ritual with specific timings. All of these have no basis in the Sunnah and are prohibited. The visit should be simple — greeting, doa, and reflection. Women visiting graves is a matter of scholarly discussion, with some scholars permitting it if done properly and without excessive wailing.