Hadhrat ibn Mas’ood (Radhiallaahu Ánhu) narrates that Rasulullah ? said, ‘I used to stop you from visiting graves, (now) visit the graves for it creates abstention (in love) for the world and reminds of the hereafter. (Mishkaat p. 154 - Qadeemi)
From the above Ahaadith, it is clear that visiting graves was prohibited for all (men and women), and thereafter the prohibition was abrogated. Therefore, it is Mustahabb for males to visit graves. With regard to females, see the following Ahaadith:
Hadhrat Abu Huryara (Radhiallaahu Ánhu) narrates that Rasulullah (?) cursed women who visit the graveyards. (Mishkaat p. 154 - Qadeemi)
Hadhrat Aaisha (Radhiallaahu Ánha) said to Rasulullah ? ‘Oh Prophet of Allah what should I say when visiting the graves’, Rasulullah ? said, 'Assalaamu Alaa ahlud diyaar minal mu'mineena wal muslimeena wayarhamu Allaahu al-mustaqdimeena minnaa wal must'akhireena, wa innaa inshaa'a Allaahu bikum lalaahiqoona
While Rasulullah ? taught Hadhrat Aaisha (Radhiallaahu Ánha) the Du’a when visiting the graves, he also cursed females from doing so.
The reason for prohibiting females from visiting graves is due to our corrupted environment wherein women (ly) who do not adhere to the laws of Hijaab and hence strange men are attracted towards them. Therefore, it is not permissible for young females to visit the graveyard.
However, if the laws of the Shariah (Hijaab, intermingling, crying, etc.) are not violated then the prohibition may be set aside. Since generally the women visiting the graves are not free from the above aspects, they should refrain from doing so.
If a female visits the grave to remember death and soften without crying … there is nothing wrong if they are old. It is Makrooh (not permissible) if they are young (Shaami vol. 1 p. 665 - Maajidiyya)
and Allah Ta'ala Knows Best
Mufti Ebrahim Desai