Updated

Malaysia's historic change of government paves the way for an extraordinary comeback for jailed opposition icon Anwar Ibrahim, who played a key role in helping to secure the election victory and has become a prime minister in waiting.

Anwar, 70, is expected to walk free Wednesday after obtaining a royal pardon but his expected return to politics could cause tensions in the new government led by his former foe, Mahathir Mohamad.

Mahathir, who became the world's oldest elected leader at 92 after national elections last week and previously served as prime minister for 22 years until 2003, has agreed to hand the baton to Anwar, but no timing has been given, creating uncertainty about how the two will work together in the interim.

Once a high flyer in the ruling party, Anwar was convicted of homosexual sodomy — and an additional charge of corruption — in 1998 amid a power struggle with Mahathir, at the peak of his authoritarian run in power. The firebrand politician was convicted of sodomy a second time in 2015, this time as his opposition alliance was making gains on the long-ruling coalition.

Anwar and his supporters have long denied the sodomy allegations, saying they were concocted to crush him and his political allies.

Yet rather than give up, Anwar worked from his prison cell to forge a new opposition alliance by ending the two-decade feud with Mahathir — a gamble that paid off when the alliance won May 9 polls and ended the National Front's 60-year grip on power.

After he was sworn in last week as Malaysia's seventh prime minister, Mahathir said the king indicated he would give Anwar a full pardon that will allow him to immediately run for office. Anwar's prison sentence ends June 8, but without a royal pardon he would be barred from politics for five years.

His daughter, lawmaker Nurul Izzah Anwar, said the Pardons Board will meet on Wednesday, after which Anwar is expected to be released. She said the full pardon for her father, who is now in a hospital recovering from shoulder surgery, was sought due to a "miscarriage of justice."

"The pardon timing is fast, and this speaks to the recognition that his conviction was unjust. It is also the recognition that the people have spoken and the royalty are responding to these issues. Mahathir is keeping to his deal," said Bridget Welsh, a political science professor at the John Cabot University in Rome.

Before he could become prime minister, Anwar must first contest a by-election to become a member of parliament. His wife Wan Azizah Wan Ismail, who heads Anwar's party, is currently the new deputy prime minister. Wan Azizah has said time is need for the country's "healing process" after the first transition of power in six decades and that Anwar needs to recover fully from recent surgery before jumping back to politics

Analysts said tensions in the new government are possible due to the dominance of the two leaders — Mahathir is the chairman of the alliance and Anwar is its de facto leader.

Cracks have already emerged, with officials from Anwar's party alleging that Mahathir bulldozed the announcement of three top Cabinet posts. Mahathir on Saturday named the finance, home affairs and defense ministers, who are leaders in his four-party alliance. He was initially supposed to name 10 ministers but appeared to have been stalled by horse-trading over positions.

The two leaders played down the rift after a meeting Saturday at the hospital. Anwar in a statement Sunday reiterated his party's support for Mahathir's leadership but called for decisions to be made jointly.

Mahathir said it would be difficult to achieve consensus and any decisions will be based on the voice of the majority. On Monday, the nonagenarian said he will meet party leaders again to discuss the Cabinet formation but reiterated in clear terms that as prime minister, he will make the final decision, a statement which could make his relationship with Anwar uneasy.

"The Anwar and Mahathir relationship will be a challenge to navigate as trust, ambition and priorities will be likely problem areas," said Welsh, a Southeast Asia expert who was in Malaysia to monitor the polls.

A popular Islamic student leader, Anwar joined the ruling Malay party in 1982, a year after Mahathir became prime minister. He quickly rose up the ranks, becoming finance minister in 1991 and deputy prime minister two years later. Anwar became Mahathir's heir apparent but they fell out during the 1998 Asian financial crisis and Anwar was sacked.

He refused to go quietly, leading tens of thousands of demonstrators in street rallies in Kuala Lumpur demanding reforms before being arrested. He was later produced in court with a black eye from a beating by the police chief. That black eye has become the logo of his People's Justice Party.

Anwar was freed in 2004 and cleared of the sodomy charges after Mahathir retired. He led an opposition alliance to major gains in the last two elections, winning the popular vote in 2013.

But in 2015, Anwar was imprisoned again after new allegations of sodomizing an aide that he and his supporters said were a government plot to crush the opposition.

"They want to end Anwar's political career but they underestimated the wrath of the people," Anwar said at the time. "Authoritarian leaders always believe the best way to deal with dissidents is to jail them, but throughout history, it has always backfired."

His opposition alliance broke up after its Islamist ally left. It regrouped as the Alliance of Hope but it was Mahathir's political comeback and his embrace by Anwar's party that helped seal the opposition's victory.

Mahathir was spurred out of retirement by a corruption scandal at 1MDB, a state fund that is being investigated by the U.S. and several other countries after associates of defeated Prime Minister Najib Razak allegedly stole and laundered $4.5 billion from it. The fund was set up by Najib in 2009 to promote economic development but accumulated billions in debts.