The deal was sealed after a meeting between President Mohammed Morsi and the prosecutor-general. It was decided that Mahmoud will remain in office until retirement age, according to Al-Jazeera.
Morsi had ordered prosecutor-general, who was appointed under toppled President Hosni Mubarak, to step down in an apparent bid to appease public anger over the acquittals of ex-regime officials on trial for a deadly crackdown on protesters during the uprising last year that ousted the long-time strongman.
Morsi had offered Mahmoud to become the country's ambassador to the Vatican. Mahmoud, however, rejected the offer.
Mahmoud said he would not leave his post despite official pressure for him to step down.
"I refuse to work anywhere else except as prosecutor- general, even if I was offered a ministerial position," Mahmoud was quoted as saying by the MENA press agency.
Earlier in the day, the prosecutor general entered his office flanked by hundreds of judicial officials.