A week after a general election rocked by suspicions of fraud, the dust is beginning to settle. It looks all but certain that Imran Khan, a former captain of Pakistan's cricket team, will be sworn in as the next prime minister. His party, Pakistan Tehreek-e-Insaf (PTI), will dominate the legislature.

The outgoing Pakistan Muslim League-Nawaz (PML-N) and the Pakistan Peoples Party cried foul, noting that the army had come out strongly in Khan's favor, muzzling the media and sending security agents to polling stations.

But the fact that these two ancient rivals are now making common cause as the loyal opposition suggests that they accept the result. So for the second time in Pakistani history, power has been democratically transferred.

For more than two decades, Khan has railed against a sleazy system of hereditary politicians and patronage networks. Yet this is the first time the PTI has shown a broadly national appeal in a country of 207 million. Its 4 million more votes than the PML-N represent a notable popular victory, one only partly undermined by vote-rigging allegations. Most remarkable is the PTI's win in Karachi, a city of powerful local machines and thuggish street politics. The PTI may yet wrest Punjab, the country's breadbasket and most populous province, from the PML-N.

That would cap a remarkable fall for the Sharif brothers: Nawaz, the "Lion of Punjab," who was prime minister until last year and is now in jail facing corruption charges, and Shahbaz, Punjab's former chief minister.

Khan, who now commands about 115 seats in the National Assembly, still needs a handful of allies — independents and smaller parties — in order to govern. The PTI's chief bankroller, Jahangir Tareen, has been flattering independents by flying them to Islamabad, the capital, on his private jet. The promises to them are the kind of thing Khan used to decry.

His wooing of the Muttahida Qaumi Movement, or MQM, the most unsavory of Karachi's parties, is making some PTI leaders gag. But at least it means that Khan does not need radical Islamist parties to form a governing coalition. Before the election, he pandered to zealots.

Meanwhile, there is no time to lose for the economy. Not for the first time, an incoming government faces a balance-of-payments crisis. The current-account deficit has widened and the currency is sliding. Pakistan imports three-quarters of all its energy needs.

Yet foreign-exchange reserves are down to just $9 billion — barely two months' import cover. An IMF bailout, of perhaps $12 billion, looks all but inevitable. Negotiating one will require finesse.

Pakistan is in hock to China which, through a ballyhooed China-Pakistan Economic Corridor, has promised $62 billion of infrastructure spending. Last week, Secretary of State Mike Pompeo said the U.S. would block any IMF bailout that profited China.

The task of stitching a deal together will fall to Asad Umar, Khan's probable finance minister. The former head of Engro, the country's biggest private conglomerate, Umar is reform-minded and admired.

The terms of an IMF deal will bring a populist party down to earth — so much for Khan's wild promises of an "Islamic welfare state." The next challenge is the electricity sector. In office, the PML-N did much to fix the notorious blackouts, helped by China building new capacity.

But a tangle of debts among state generators, energy suppliers and banks has been exacerbated by theft from the grid. This can be resolved by reducing subsidies, raising energy taxes and recapitalizing state entities. Khan has long bemoaned Pakistan's institutional decay. Renewal starts with fixing the electricity mess.

Then come security and foreign policy. Islamist violence marred the election and is a constant threat. Meanwhile, the regional situation grows trickier, with rivalry between the U.S. and China, and China and India. That comes on top of rocky relations with the U.S. itself, the festering sore of war-torn Afghanistan to the northwest, and Pakistan's age-old and bitter animosity toward India.

Khan would seem ill-suited for these challenges. He has been more critical of the U.S., especially over its use of drones to kill jihadists, than of the extremists themselves. And he is friendly with an army that is the chief obstacle to better relations with India.

For now, Khan, who has described parliament as "the most boring place on earth," must find a sense of dedication, detail and compromise that has evaded him till now. He must learn to work with a political class he has only slammed. And he must gently let down his most enthusiastic supporters from the irresponsible highs he generated for them — for instance, by promising to end corruption within 90 days. It will require dogged strength, which he has in abundance, and humility — which, equally, he lacks.