Military authorities in Zimbabwe have agreed to grant the former president Robert Mugabe immunity from prosecution and told him his safety will be protected in his home country.

Mugabe, who ruled Zimbabwe with an iron fist for 37 years, resigned on Tuesday hours after parliament launched proceedings to impeach him. He had refused to leave office during eight days of uncertainty that began with a military takeover.

Emmerson Mnangagwa, the former vice-president sacked by Mugabe earlier this month, is to be sworn in as president on Friday.

Robert Mugabe planned for everything, except his own retirement. He outsmarted his rivals and blindsided his allies for decades, and so most of the country had come to accept that he would stay in office until the day he died.

Emmerson Mnangagwa, now expected to take over from him, had boasted in May that Mugabe would die in the job. As such, there was no planning for the future.

One of the biggest questions now will be whether Mugabe can stay in Zimbabwe. Few ousted autocrats are allowed to stay on in the countries they once ruled, due to fears they may resuscitate their careers or become figureheads for opposition.

Mugabe may be an exception, because of his age and his role at the heart of Zimbabwe’s long liberation struggle. Throughout the most unusual of coups, Mugabe appears to have been treated with extreme deference by the generals holding him prisoner. Party members who ended his career have paid tribute to his historical achievements.

His much-hated wife, dubbed Gucci Grace for her spendthrift habits, may be targeted. But Mugabe is unlikely to want to face retirement alone, and any deal is expected to include protections for Grace and their children.

Wherever they end up, the Mugabes will certainly not lack for pension funds. By some estimates he holds about £1bn of assets, including vast property holdings and businesses around the country. Much of his wealth has also been invested outside Zimbabwe.

A government source confirmed to Reuters that Mugabe had told negotiators he wanted to die in Zimbabwe and had no plans to live in exile.

“For him it was very important that he be guaranteed security to stay in the country ... although that will not stop him from travelling abroad when he wants to or has to,” the source said.

A second source said: “The outgoing president is obviously aware of the public hostility to his wife [Grace], the anger in some circles about the manner in which she conducted herself and approached Zanu-PF party politics.”

“In that regard, it became necessary to also assure him that his whole family, including the wife, would be safe and secure.”

“It was very emotional for him and he was forceful about it,” said the second source, who was not authorised to speak on the details of the negotiated settlement.

There is still much residual respect for Mugabe, and many in Harare say he should be allowed to “rest” rather than face charges or enforced exile.

Zanu-PF officials told the Guardian that Mugabe and Grace will be allowed to live in peace.

Ziyambi Ziyambi, a Zanu-PF MP and former minister, said both had been guaranteed immunity from prosecution and other unspecified protections.

“There has been an agreement. They are elder statesmen and will be respected and given their dues. He was our president and he agreed to resign, so he will enjoy the benefits of being an ex-president and his wife too. He is our icon,” Ziyambi said.

Speaking on Wednesday, Mnangagwa, 75, a liberation war veteran, said the country was witnessing a “new and unfolding democracy” as he returned to a jubilant welcome two weeks after fleeing to South Africa.

Mugabe had angered many Zimbabweans when he did not resign in a televised address on Sunday, as many had anticipated.

The government source said the tipping point for him was the realisation that he would be impeached and ousted in an undignified way. “When the process started, he then realised he had lost the party,” the source said.

Mugabe will receive a retirement package that includes a pension, housing, holiday and transport allowance, health insurance, limited air travel and security.

The generosity of the package will anger many in Zimbabwe, where unemployment is as high as 80%, many live in deep poverty and opposition has been brutally repressed over decades.

Story continues