KUALA LUMPUR, Malaysia — Just a few months ago, the political machine led by Najib Razak, the gilded prime minister of Malaysia, appeared so indestructible that a multibillion-dollar corruption scandal seemed unlikely to derail it. The end came so quickly, so completely, that even his opponents were shocked.

For nearly a decade, Mr. Najib, 64, had unfettered control of his nation’s courts and coffers. His party had thrived by unfailingly delivering huge cash handouts at election time. The media was at his disposal; journalists he didn’t like, he shut down. Political foes were shoved into prison.

The pampered son of a prime minister and nephew of another, Mr. Najib enjoyed the friendship of President Trump, who after playing golf with him in 2014 gave him a photo inscribed, “To my favorite prime minister.” Last year, Mr. Trump hosted Mr. Najib at the White House, even as the United States Justice Department accused him of taking Malaysian state money.

But his authority suddenly evaporated in the early hours on May 9, after Malaysia’s national elections delivered a commanding majority to the opposition, now led by the political titan who had once lifted Mr. Najib to power: the 92-year-old Mahathir Mohamad.