KUALA LUMPUR — Malaysia’s new prime minister, Mahathir Mohamad, said on Friday that the country’s king had agreed to pardon the imprisoned opposition leader Anwar Ibrahim, potentially clearing the way for fulfilling one of the central pledges of Mr. Mahathir’s campaign this year.

The announcement is the latest turnaround in an election that has upended Malaysian politics. Mr. Mahathir, who also served as prime minister from 1981 to 2003, led a coalition of opposition parties to defeat Prime Minister Najib Razak, his former protégé, who has been accused of large-scale corruption.

The opposition victory ended a grip on power for Mr. Mahathir’s former party, the United Malays National Organization, that dated to the country’s independence from Britain in 1957.

Mr. Anwar was sentenced to a five-year prison term on a sodomy conviction in 2014 and is nominally scheduled for release in June. The sentence is seen as a second politically motivated case against him: He was previously imprisoned on corruption and sodomy charges after falling out with Mr. Mahathir in 1998.