The opposition leader and reformist icon Anwar Ibrahim has been issued a full royal pardon and released from detention with immediate effect in Malaysia, following a stunning election where the ruling party of six decades was thrown out at the ballot box.

Mr Anwar, 70, waved to supporters and media as he left hospital on Wednesday, where he was recovering from a minor surgery. He had been serving his second stint in jail since 2015 on charges of sodomy, a conviction he says was politically motivated as he challenged the authority of now-ousted prime minister Najib Razak.

He is now expected to take over as the country’s prime minister from election victor Mahathir Mohamad. Mr Mahathir, who will soon turn 93, campaigned on the basis of releasing and empowering Mr Anwar, though the handover is unlikely to happen immediately.

Appearing at a news conference on Wednesday, Mr Anwar said he would take some time off with his family, saying he would give full support to the government of Mr Mahathir without becoming a part of it “for now”. Asked about when he would make his political comeback, he told reporters: “I’ve never left the scene.”

Mr Mahathir, who first led the country as an authoritarian prime minister for 22 years until 2003, said on Tuesday that he planned to run the country for “one to two years” to fix Malaysia’s financial problems. Analysts have previously told The Independent that this would likely work in Mr Anwar’s favour in the long run, but there remains uncertainty over how the two will work together in the meantime.

“I have given my assurance, I am here as a concerned citizen to give complete support to manage the country on the understanding that we are committed to the reform agenda, beginning with the judiciary, media and the entire apparatus,” Mr Anwar told the news conference at his home.

Once a rising star in the ruling party, Mr Anwar was first convicted of homosexual sodomy and corruption after a power struggle with Mr Mahathir himself back in 1998.

Mr Anwar and his supporters have long denied the sodomy allegations, saying they were concocted to destroy his political career, and point to the fact that Malaysia’s strict sodomy laws are rarely enforced in practice.

Rather than give up, Mr Anwar worked from his prison cell to forge a new opposition alliance by ending the two-decade feud with Mr Mahathir – a gamble that paid off when their coalition won the 9 May polls, ousting a government that had become increasingly unpopular following a corruption scandal and hastily-imposed sales tax.

“Many have asked me how it is that our reform movement has now joined forces with the very same former dictator, Mahathir Mohamad, who sacked my father in 1998 and saw him arrested, brutalised and incarcerated,” Mr Anwar’s daughter, Nurul Izzah, wrote in an article for The Guardian newspaper on Tuesday.

“My answer is simply that we must all firmly resolve to never let our nation sink to the depths it did again and Prime Minister Mohamad now has a rare second chance to put things right.”

The royal palace said in a statement that the country’s constitutional monarch had given Mr Anwar a full pardon following advice from the pardons board. He still needs to contest a byelection to become a member of parliament before he can replace Mr Mahathir.

Mr Anwar’s wife, Wan Azizah Wan Ismail, who is the new deputy prime minister and the first woman to hold that title in Malaysia’s history, has said there is no rush for her husband to take over.

She said time is needed for the country’s “healing process” after the first transition of power in six decades, and that Mr Anwar should recover fully from recent surgery before jumping back into politics.