In the capital Kuala Lumpur, Anwar's supporters were in a celebratory mood.
"I got goosebumps, seriously," said 36-year-old Norhafitzah Ashruff Hassan. "He fought hard to be given the chance to be PM. I hope he performs well and proves his worth."
Muhammad Taufiq Zamri, a 37-year-old product manager said: "I cannot express in words the ecstatic feeling I have."
Campaigning on an anti-graft message, Anwar's multi-ethnic Pakatan Harapan coalition won the most seats in the weekend's election with 82.
But it still fell short of the 112 required for a majority.
In an attempt to break the deadlock, the king had summoned Anwar and former prime minister Muhyiddin Yassin, whose Perikatan Nasional bloc came in second place with 73 seats. But no deal could be struck.
The king held a special meeting with other royals earlier Thursday before the palace announced Anwar as the new premier.
For Anwar, the premiership is the culmination of a 25-year rollercoaster.
The firebrand former student activist was first poised to take the reins in the late 1990s, after serving as finance chief and deputy prime minister under Malaysia's political patriarch Mahathir Mohamad.
But the two had a bitter falling-out over how to handle the 1997-98 Asian financial crisis.
Mahathir sacked his former protege, who was also expelled from their then-party the United Malays National Organisation (UMNO), and charged with corruption and sodomy -- charges Anwar said were politically motivated.
Anwar was sentenced to six years in jail for corruption in 1999, and given an additional nine on a sodomy charge the following year.
Street protests erupted and evolved into a movement for democratic reforms, with Anwar stringing together an opposition coalition from behind bars.
The Mahathir-Anwar tussle has dominated and shaped Malaysian politics over the past four decades, "alternately bringing despair and hope, progress and regress to the country's polity", according to Oh Ei Sun of the Pacific Research Center of Malaysia.
The Malaysian Supreme Court overturned Anwar's sodomy conviction in 2004 and ordered him freed.
'Long time coming'
Anwar re-aligned with Mahathir during the 2018 elections, when his erstwhile foe came out of retirement to challenge incumbent Najib Razak, who was mired in the billion-dollar 1MDB financial scandal.
Their detente scored a historic victory against UMNO and Najib, who is now serving a 12-year jail term for corruption.
Mahathir became prime minister for the second time, with an agreement to eventually hand the premiership to Anwar.
He never fulfilled that pact, and their alliance collapsed after 22 months.
In his most recent bid to lead Southeast Asia's third-largest economy, Anwar once again pledged to end corruption and cultivate multi-ethnic harmony.
"This is a long time coming for Anwar Ibrahim," Asrul Hadi Abdullah Sani, deputy managing director at strategic advisory firm Bower Group Asia, told AFP.
"One of his agendas is to ensure he is able to fulfil his reform agenda as he looks to stabilise a loosely cobbled federal coalition."
James Chin, a professor of Asian studies at the University of Tasmania, told AFP the announcement "will be welcomed internationally since Anwar is known as a Muslim democrat worldwide".
"His biggest challenge will be to lead Malaysia out of the economic malaise following the pandemic."