KUALA LUMPUR, Malaysia — Twenty years ago, burning from his dismissal as deputy prime minister of Malaysia, Anwar Ibrahim unleashed a protest movement against his mentor, Prime Minister Mahathir Mohamad, calling him insane, senile and unfit to lead the nation.
Today, the two men, after years of bad blood and the imprisonment of Mr. Anwar, have retaken their places at the top of Malaysian politics: Mr. Mahathir is prime minister once again, at age 92, and Mr. Anwar, newly pardoned by the country’s king on Wednesday, is waiting to inherit the leadership.
This time, they have done it under the banner of what had been Malaysia’s opposition, the Alliance of Hope, which decisively swept away the government of Najib Razak in national elections last week by accusing him of epic corruption and vowing reform — and the reinstatement of Mr. Anwar’s political career.
Speaking to reporters on Wednesday, Mr. Anwar, 70, newly free after five years of detention, vowed that ending the influence of politics over the country’s justice system would be a priority.
“We must stop this once and for all,” he said. “The most significant lesson one can learn from prison life is the value of freedom.”
He has twice been deprived of his freedom, both times in what was widely seen as political manipulation coming from the leadership of the governing party, the United Malays National Organization, or UMNO, which at one point included Mr. Mahathir, Mr. Anwar and Mr. Najib among its top ranks.
Mr. Anwar had been a top deputy to Mr. Mahathir, who was prime minister from 1981 to 2003, and was seen as his likely successor. But after a falling-out between the two in 1998, over how to respond to the Asian financial crisis, Mr. Mahathir fired Mr. Anwar as deputy prime minister.
He began his protest movement against Mr. Mahathir and UMNO, and was quickly arrested and convicted of corruption and sodomy. Human rights groups and his supporters say those cases were trumped-up at the behest of Mr. Mahathir, who after the arrest publicly condemned Mr. Anwar, saying, “I cannot accept a man who is a sodomist as leader of the country.”
Mr. Anwar was released in 2004 and elected to Parliament in 2008 after the five-year restriction on his return to politics expired. That year, he was again accused of sodomy under a British-era law that human rights groups say is archaic. That case was also seen as being manipulated by his political rivals, including Mr. Najib, who became prime minister in 2009.
After years of suspension and legal jeopardy, he was sentenced to five years in prison on a sodomy charge in 2015. Originally scheduled to leave detention in June, he instead walked free on Wednesday morning.
He left Cheras Rehabilitation Hospital in Kuala Lumpur, the capital, where he was recovering from shoulder surgery, at 11:30 a.m. along with his wife, Dr. Wan Azizah Wan Ismail, who is to become deputy prime minister.
Mobbed by well-wishers, the two got into a vehicle together to travel to the royal palace, where they had an audience with Malaysia’s king, Sultan Muhammad V, who made it official: Mr. Anwar had been fully pardoned, lifting a statutory five-year ban on his political activity and allowing him to pursue elected office quickly.
The ouster of Mr. Najib last week marked the first time UMNO had been out of power since Malaysia’s independence from Britain in 1957. Many voters said they were upset about reports of widespread corruption, including accusations of the misappropriation of billions of dollars from 1Malaysia Development Berhad, a state fund he once led.
Mr. Najib and his wife have been barred from leaving the country, and Mr. Mahathir said he would be investigated. Officials accused of covering up the scandal have stepped down or were suspended in some of the first official acts of the new government.
On Tuesday evening, the government released the executive summary of a 2016 report by the country’s Auditor General, a ministry, which Mr. Mahathir said Saturday would be declassified. While the contents of the report had been previously reported, the release showed the new commitment to transparency on the case. The Auditor General’s website was down most of the evening, apparently from high traffic.
While Mr. Anwar is now free, much more must happen before he could become prime minister. First, he must become a member of Parliament. Dr. Azizah has said in recent months that she could step down and allow him to run for her seat in a by-election, just as she did in 2008.
“I am overjoyed with his release,” said Maria Chin Abdullah, an advocate of government reform who was elected to Parliament last week.
“The nation is ready to enter into its democratic reformation,” she said. “We certainly need someone like Anwar to help heal this nation and unite the various parties.”
Mr. Anwar gave little indication of when he might return to office. He said that he planned to first travel and give lectures at universities around the world.
“I think I have a small contribution to make to show that the voice of reason and moderation in Islam is paramount, and, No. 2, that Muslims can also be counted upon to ensure that there is freedom and justice for all citizens,” he said.
Mr. Anwar said he had forgiven Mr. Mahathir for turning on him two decades ago.
“He supports the reform agenda,” Mr. Anwar said. “He has facilitated my release. Why would I harbor any malice toward him?”
Despite Mr. Mahathir’s autocratic past, many of his former rivals have said they believe Dr. M, as the physician turned politician is informally known, is a changed leader, more concerned with polishing his legacy than amassing power.
But he and Mr. Anwar must still show they can cooperate, said Terence Gomez, a professor of political economy at the University of Malaya in Kuala Lumpur.
“Is this an alliance where these two men are comfortable with each other? I don’t think so,” he said. “They came together because of the common need to get rid of Najib. Having succeeded in doing so, now comes the difficult part of reinventing their relationship to one that is truly workable.”
Sharon Tan contributed reporting.