Former Peruvian president Alan Garcia has died after shooting himself moments after police attempted to detain him amid corruption allegations.

President Martin Vizcarra announced on Twitter that Garcia died after undergoing emergency surgery hours earlier.

Doctors at the Jose Casimiro Ulloa Hospital in the capital city of Lima said they provided cardiac resuscitation three times while trying to save his life.

A supporter of former Peruvian President Alan Garcia grieves after learning of his deathA supporter of former Peruvian President Alan Garcia grieves after learning of his death (AP Photo/Martin Mejia)

“Distraught over the death of ex-president Alan Garcia,” Mr Vizcarra wrote. “I send my condolences to his family and loved ones.”

The shocking end for the man who twice ruled Peru but more recently was ensnared in a corruption scandal comes amid national upheaval over endemic corruption involving nearly every former living president.

Mr Garcia repeatedly professed his innocence and said he was the victim of false evidence by political enemies who accused him of taking more than 100,000 US dollars (£76,000) from Brazilian construction giant Odebrecht.

Odebrecht admitted in a 2016 plea agreement with the US Justice Department that it paid nearly 800 million dollars (£613 million) throughout Latin America in exchange for lucrative public works contracts.

Garcia led the country twice (AP Photo/Karel Navarro, File)

The 69-year-old former head of state’s lawyer accused authorities of neglecting to provide Garcia information on why he was being detained or show their official identifications when they showed up to arrest him.

“The president, upset over this situation, knowing his absolute innocence … had this terrible accident,” lawyer Erasmo Reyna said.

Interior minister Carlos Moran said police found Garcaa on the second floor of his home when they arrived. He asked for a moment to call his lawyer, entering a bedroom of the leafy mansion in Miraflores and closing the door behind him.

Peru’s police officers stand guard at the hospital where former president Alan Garcia was takenPeru’s police officers stand guard at the hospital where Garcia was taken (AP Photo/Martin Mejia)

Mr Moran said: “Minutes later the sound of gunfire was heard. Police forced their way into the room and found him.”

He was immediately transported to the hospital, arriving at 6.45am local time. A team of doctors, some called from outside hospitals, began operating at 7.10am, according to Peru’s health ministry.

“The situation is very critical,” health minister Zulema Tomas said shortly after the incident. “It’s grave.”

Health minister Zulema Tomas, centre, speaks at a news conference at the Casimiro Ulloa hospital where Garcia was taken (AP Photo/Martin Mejia)

The stunning turn of events comes four months after Garcia tried to seek asylum in Uruguay as prosecutors in Peru investigated allegations he illegally took payments from Odebrecht. He remained there for a little more than two weeks before having his request denied.

In rejecting his claim, the South American nation’s embassy said there was no evidence to support Garcia’s contention that he was being targeted politically.

“In Peru, the three branches of government function freely and autonomously, especially in the case of judicial power,” Uruguayan President Tabare Vazquez said.

Peru has gone further than any other country outside Brazil in prosecuting politicians tied to the Odebrecht probe. All but one living former head of state is being investigated for corruption tied to the scandal.