Former Malaysian opposition leader Anwar Ibrahim released after years in prison

Former Malaysian opposition leader Anwar Ibrahim — the man who may replace prime minister-elect Mahathir Mohamad — has just been released from prison.

Mr Anwar, who was formerly Dr Mahathir's political rival, has been promised leadership of Malaysia's new governing coalition when Dr Mahathir, 92, decides to step down.

According to a palace statement Dr Mahathir's former deputy received a full pardon.

"The King, with the advice of the pardons board of the federal territories of Kuala Lumpur, Labuan and Putrajaya, has allowed a full pardon and given an immediate release for … Anwar bin Ibrahim," a statement issued by the palace said.

After leaving the Cheras Rehabilitation Centre in Kuala Lumpur, where he was recovering from a shoulder injury, Mr Anwar has now travelled to the palace to meet with King Sultan Muhammad V.

He was greeted at the palace by Dr Mahathir, his ally-turned-foe-turned-ally, with whom he joined forces to oust scandal-tainted Najib Razak's administration in last week's election.

Their newfound alliance is a stunning turnaround from the 1990s when — on the orders of Dr Mahathir and his government — Mr Anwar, arrested and jailed on charges of corruption and sodomy.

He was jailed again in 2015, under the reign of Mr Razak.

Mr Anwar always denied the charges and claimed he was a victim of a political conspiracy.

Smiling and waving to supporters, Mr Anwar, 70, was wearing a black suit with a tie, surrounded by his family, lawyers and prison guards.

Foreign Affairs Minister Julie Bishop said in a statement that Australia welcomed Mr Anwar's full pardon and release.

"We have followed Anwar's trial and incarceration closely, and have expressed concern to the previous Malaysian government," she said in a statement.

"We are pleased that the royal pardon will allow Anwar to be reunited with his family in time for Ramadan and again participate fully in Malaysia's politics and governance.

"Anwar has much to contribute to Malaysia."

Mr Anwar's freedom is expected to ease fears of a growing rift within the newly-formed coalition government, after differences erupted between supporters of Dr Mahathir and Mr Anwar over cabinet positions and Mr Anwar's role in the party.

The volatile relationship between Dr Mahathir and Mr Anwar has dominated Malaysia's political landscape for over three decades and is central to the future of the alliance.