CAIRO — In his final years, Hosni Mubarak basked in the affection of his family and his supporters.

The former autocrat, who ruled Egypt for three decades, was photographed at a Mediterranean beach resort with a smiling granddaughter on his knee. His sons, once viewed as pariahs by many Egyptians, appeared at nightclubs and soccer matches.

Last month, as Mr. Mubarak neared death, his grandson Omar posted a photograph to Instagram that showed him kissing the forehead of his grandfather. “All love and appreciation,” it read, with a heart emoji.

Mr. Mubarak’s comforting end was a sharp contrast with the final days of his successor as president of Egypt, Mohammed Morsi. Although democratically elected in 2012, Mr. Morsi was ousted a year later, cast into a maximum-security prison where he was denied essential medicines and regular family visits. He died last June after collapsing inside a cage in a courtroom.

The clashing fates of the two men underscore the complex task facing Egyptians as they weigh the legacy of Mr. Mubarak, who divides them in death as in life. Mr. Mubarak, who died on Tuesday at 91, will be buried with full military honors at a state funeral in Cairo on Wednesday.