Image copyright AFP Image caption The pardoning could lead to Anwar Ibrahim, shown here with his wife Wan Azizah, eventually taking office

Malaysia's king has agreed to pardon a politician whose case has gripped national politics for two decades, says new PM Mahathir Mohamad.

Anwar Ibrahim, once considered a potential future leader, was jailed on charges of corruption and sodomy after falling out with the government.

But Mr Mahathir, the PM under which he was first jailed, just won an election on a pledge of freeing him.

He has indicated he will hand power to Anwar within a few years.

At a news conference on Friday - a day after his historic election win - Mr Mahathir said the king "has indicated he is willing to pardon Datuk Seri Anwar immediately", using an honorific title.

"It is going to be a full pardon which of course means that he should not only be pardoned, he should be released immediately when he is pardoned. After that he will be free to participate fully in politics."

Anwar's wife, Wan Azizah, said the king wanted to grant the pardon as soon as possible and "if the director of prisons is satisfied... then [Anwar] may be released in two to three days".

Mr Mahathir's election win, at the head of the Pakatan Harapan, ended more than six decades of rule by the Barisan Nasional (BN) coalition.

Both he and Anwar were formerly in power, as part of the BN, as prime minister and deputy respectively. The story of their relationship is one of extraordinary twists and turns.

Anwar was sacked in 1998 after a dispute with the coalition leadership and then led huge protests against Mr Mahathir's government. He was jailed a year later for abuse of power.

Then in 2000 he was convicted of sodomy, and given an additional nine-year-term.

Though sodomy is illegal in conservative Muslim Malaysia people are seldom convicted for it, so his case was widely seen as an attempt by the government to remove a political threat.

In 2004 his conviction was overturned and he led the opposition to unprecedented gains - though not victory - in the 2008 and 2013 general elections.

Media playback is unsupported on your device Media caption Malaysian voters react in 2018 to Mahathir Mohamad's victory over Najib Razak

But that acquittal was itself overturned a year later - as he was preparing to fight a state election he seemed likely to win - and he was sent back to jail.

In a shock turn of events earlier this year, his former nemesis Mahathir Mohamad announced he was going to be running for the top office again.

He said he was sick of the corruption allegations plaguing the incumbent Najib Razak, another of Mr Mahathir's former allies.

Anwar has remained popular with opposition supporters, and a condition of Mr Mahathir being allowed to lead the coalition was that he agreed to secure a royal pardon.

He agreed, and further said he aimed to hand the prime minister post to Anwar within two years.

It remains unclear when the pardon will be issued, and Mr Mahathir warned his supporters that the process of Anwar becoming an MP again so he can take on the leadership "might take a long time".

Will Mahathir keep his word?

Jonathan Head, BBC South East Asia correspondent

Some have likened the twists and turns of Malaysian politics to a plot by Shakespeare. Mahathir Mohamad, the autocratic strongman who turned on his chosen successor Anwar Ibrahim, is now allied with him and seeking his release, having deposed Najib Razak, another one-time Mahathir protégé.

During the bitter election, Mr Najib called the alliance a "coalition of convenience" which, he warned, would fall apart if they won.

There are many strong personalities in the victorious coalition which will have to be reconciled, but none more important than Anwar and Mr Mahathir, who until his defection to the opposition, still described Anwar as morally unfit to lead the country.

Will the famously devious Mr Mahathir keep his word? Everything he says suggests he will. He keeps repeating the pledge to push for a pardon, and to step aside for Anwar.

Even with his remarkable fitness, he must, at 92 years old, consider his physical limitations. They're in coalition, but with only 13 seats in parliament Mr Mahathir's new party Bersatu is much smaller than Anwar's Keadilan, with 47. The days where Mr Mahathir could dictate his terms are probably over.