Anwar Ibrahim. AFP

Anwar Ibrahim, a former Malaysian Deputy Prime Minister, has walked free after being granted a pardon for sodomy by the country’s king.

The royal pardon potentially paves the way for him to succeed the country’s elderly new leader┬áMahathir Mohamad, who was the surprise victor over former Prime Minister Najib Razak in last week’s parliamentary election.

Surrounded by media and supporters, Anwar walked out of a hospital Wednesday where he was recovering from surgery.

Anwar had been serving a five-year prison sentence for sodomy charges he says were politically motivated. Until his early release, he had been scheduled to be freed in early June.

The politician, a voice for reform in Malaysia and once a leading light in the party that ruled Malaysia until last week’s election, was jailed in 2015. It was the second time he had been convicted of a sodomy offense.

Mahathir, 92, and Anwar, 70, had put aside a longstanding rivalry to fight together to defeat Najib.

Anwar’s wife, Wan Azizah Wan Ismail, endorsed Mahathir and served as his deputy in the coalition to oust Najib.

Mahathir and Anwar are a political odd couple who have “hated each other politically” for two decades, James Chin, Director of the Asia Institute at the University of Tasmania, told CNN ahead of the election last week.

However, the Malaysian public saw the two politicians’ “rapprochement as something very good,” Chin said, and together they toppled Najib and broke the ruling coalition’s 60-year grip on the country.