President Mohammed Mursi of Egypt has agreed to allow the Mubarak-era chief prosecutor to keep his job after an embarrassing public row.
Spokesmen for Mr Mursi and Prosecutor-General Abdel Maguid Mahmoud confirmed an agreement had been reached.
Mr Mahmoud earlier returned to work in Cairo on Saturday, escorted by judges and lawyers. He has been criticised for acquitting officials accused of attacking protesters under former President Hosni Mubarak.
Mr Mursi on Thursday tried to remove him by appointing him ambassador to the Vatican.
Mr Mahmoud vowed to serve out his term:
"I occupy this office and I will defend myself, and I will defend my position, and I will defend the independence of the prosecutor general, and the independence of the judges, and I will not leave this office unless I am assassinated," he said.
Vice-President Mahmoud Mekki confirmed that Mr Mursi had agreed to keep Mr Mahmoud in his post.
A BBC correspondent said the outcome is a big blow for Mr Mursi and shows the limit of his power.