It should be stressed, first of all, that Islam demands Muslims to follow the guidance of Prophet Muhammad (peace and blessings be upon him) in every sphere of life. In times of joy or in times of trials when losing a relative or friend, Muslims should abide by a certain code of ethics that is based on the Qur’an and the Sunnah of the Prophet (peace and blessings be upon him). When losing a dear person, of course Muslims are allowed to express their feelings of sadness by crying without going to extremes like lamenting or moaning over the dead person.
The prominent Muslim scholar and da`iyahZeinab Mostafa stated: Almighty Allah, the Most Merciful, teaches us to adhere to the manners of Islam in times of trials when we lose a dear person. Islam strongly forbids lamenting and moaning over the deceased.
Obviously, it is allowed to cry and feel sad for losing a family member or a friend but without reaching the stage of moaning and lamenting over him or her. Prophet Muhammad (peace and blessings be upon him) has taught us what we say in such occasions and how we can express our feelings without committing a sin.
As for “the deceased being in pain” because of his or her family’s lamenting over his or her death, it is true: The Prophet (peace and blessings be upon him) said, “The deceased is caused pain by his or her family’s expression of grief over his or her death.” However, the hadith is meant for the person that (before dying, that is) either asked his or her family to cry and lament him or her upon his or her death or the person that did not teach his or her family members the right way of dealing with death and behaving after it or saw them in his or her life doing this without forbidding them.
In the aforementioned cases, the deceased is being punished for failing to teach his or her family members about the manners of Islam. However, if he or she shouldered his or her responsibility in educating and instructing them and still they did not follow the advice, then he or she feels sorry for their behaviour. This is the explanation of Ibn Hajar in his commentary on Al-Bukhari.