In Islam, burying the dead was legislated as an act of honoring the deceased, protecting their bodies from the beasts or birds of prey, and preventing the smell of decaying corpses from harming those alive.
as for the question, it is not allowed to bury a man and a woman in a single grave except in case of a necessity that warrants it.
In his response to this question, Dr. `Ali Jum`ah Muhammad, the Grand Mufti of Egypt, said,
there is a consensus among scholars on the impermissibility of burying more than one person in a single grave, unless there is a necessity, such as when there is insufficient space in the graveyard, when there is no gravedigger available, or when there is no empty grave available. Prophet Muhammad (peace and blessings be upon him) and his Companions used to bury each individual in a separate grave, except in the case of a necessity.
When there is a necessity, the most meritorious dead person should be buried in the direction of the Ka`bah, followed by the next in the degree of merit, which is estimated according to the extent to which the dead person was preferred as an Prayer leader. This agrees with a hadith reported by Al-Bukhari and narrated by Jabir, who said,
On the day of [the Battle of] Uhud, we complained to the Messenger of Allah saying, “O Messenger of Allah! It is so difficult for us to dig a grave for every [dead] person!” So, the Messenger of Allah said, “Dig deep, dig well, and bury two or three persons in a single grave.“ They [Muslims present] said, “O Messenger of Allah! Whom should we bury first?” He replied, “Bury the one who memorized more of the Qur’an.”
Jabir then said, “My father was the third of three buried in the same grave.”
a wall of earth should be made as a barrier between any two individuals buried in the same grave. A father should be buried before his son, out of respect for the father, even if the son were more meritorious. A mother should be buried before her daughter.
however, women must not be buried in the same grave with men, unless there is a confirmed necessity. If there is a confirmed necessity, then the condition of making a barrier of earth between two individuals must be fulfilled. Moreover, a male should be buried before a female (even if she is his mother), and he should be placed at the head of the grave and the female at the other end.