LAHORE, Pakistan — Imran Khan, a charismatic cricket star who has fiercely criticized American counterterrorism policy in a region plagued by extremism, appeared poised on Thursday to become Pakistan’s next prime minister.
After preliminary results showed his party decisively ahead in an election that critics say was deeply marred, Mr. Khan addressed the nation on television, outlining what he would do as prime minister.
He said he would fight corruption at the highest levels, improve relations with China, seek a “mutually beneficial” relationship with the United States and create a just welfare state along the lines of what the Prophet Muhammad did centuries ago.
“We’re going to run Pakistan in a way it’s never been run before,” he said.
He also said he would never live in the prime minister’s mansion. In a country of so many poor people, he said, “I would be embarrassed” to stay in such a house.
For years, Mr. Khan had tried but failed to take the reins of this nuclear-armed Islamic republic, which has struggled with poverty, economic stagnation and instability and which is increasingly torn between its two biggest allies: China and the United States. But this time around, he found a powerful ally in Pakistan’s military.
In recent months, army and intelligence officers pressured, threatened and blackmailed politicians from rival parties, human rights groups have said, steadily thinning out Mr. Khan’s competition.
Members of rival parties said unfair practices continued during the vote, with some ballot counting done in secret, guarded by soldiers. But Pakistan’s election authorities said the vote, on Wednesday, had been fair.
“The way this stage has been set, it would have been a surprise if he didn’t win,” said Nighat Dad, the executive director of Digital Rights Foundation, an advocacy group.
Mr. Khan’s ascendance injects a volatile element into relations with the United States under President Trump, who has accused Pakistan of lying about the Taliban, Al Qaeda and other militants embedded in Pakistan and Afghanistan.
Mr. Khan has denied that and accused the United States of recklessness and murder in its use of drone strikes on suspected extremists in his country, signaling he wants them to stop.
Friends and foes describe Mr. Khan, 65, as relentless, charming, swaggering and highly unpredictable.
As a young man, his good looks, prowess on the cricket pitch and success with women made him something of a fascination in England, where he lived for a time. In 1982, he posed for a London newspaper lounging on a bed, wearing only briefs.
“Imran Khan is worried in case I portray him as a sex symbol,” wrote the London newspaper journalist sent to interview him.
“This is possibly why Imran is stretched across his hotel bed wearing only a petulant expression and a pair of tiny, black satin shorts.”
But a complex, mysterious transformation would begin soon after. In 1992, Mr. Khan captained Pakistan’s cricket team to a World Cup victory over England, the country’s former colonial ruler. It was a moment of immense Pakistani pride, and Mr. Khan was at the center of it.
But looking back on it, he told interviewers, he felt empty.
He began to stay away from the clubs, the partying, the girlfriends. He began a quest to build a cancer hospital in Pakistan for the poor; his mother had died of cancer, and the two had been close.
He turned to Islam and the Sufi sect, which he said helped lend purpose to his life.
Then he entered politics. Pakistan in the late 1990s was a mess: Its Machiavellian spy services were working with the United States and, at the same time, supporting the Taliban and Osama bin Laden. The country was poor, troubled and divided — which could still be said today.
Mr. Khan seized on a single issue: governance.
“In Pakistan, the main problem is not extremism,” he said in a recent interview with The New York Times. “We are a governance failure. And in any third world country, the moment the governance collapses, mafias appear.”
He focused on corruption, repeatedly stating that a few political dynasties had shamelessly enriched themselves while governance weakened and the country grew poorer.
But Mr. Khan’s shouts for reform were not taken seriously at first.
The Justice Movement he founded in 1996 initially won only one seat in Parliament — his — and a Pakistani newspaper ridiculed him as “Im the Dim.”
His personal life also careened back and forth, always attracting enormous attention, especially after he married a wealthy British heiress, Jemima Goldsmith. She converted to Islam, they had two children, and tried living in Pakistan together. But it did not last, and they divorced. Mr. Khan married twice more, the last time to his spiritual healer, which again raised eyebrows across Pakistan.
But he seemed adept at not letting the gossip pages distract him, and he kept hammering on about corruption. And two years ago, he received a gift from the Panama Papers.
Those files, an avalanche of millions of leaked confidential documents from a law firm in Panama, included several pieces of information that incriminated Pakistan’s prime minister at the time, Nawaz Sharif. Evidence began to build that Mr. Sharif had stolen millions of dollars from public coffers in Pakistan to buy expensive apartments in London, under the names of his children.
Mr. Khan capitalized on this and called for Mr. Sharif to resign. The Supreme Court removed Mr. Sharif from office, and just two weeks ago, right before the election, Mr. Sharif and his daughter were imprisoned.
Few would disagree that corruption is out of control in Pakistan. But many observers here saw something in Mr. Sharif’s downfall that was more selective, possibly more sinister.
The widespread suspicion was that Pakistan’s powerful military and intelligence services had pressured the judiciary to take out Mr. Sharif, clearing a path for Mr. Khan to take over. Mr. Sharif had clashed with the army chiefs, even some of those he had chosen, many times. He was a thorn in their side.
Mr. Khan, on the other hand, was someone the military bosses seemed to think they could work with. Analysts said he shared their worldview, in which Pakistan would kowtow less to the United States and talk more with the Taliban and other extremist groups.
In the prelude to the election, the military seemed to push even harder for Mr. Khan. Human rights groups, academics and members of other political parties said security officers threatened politicians to defect to Mr. Khan’s side. Several did.
That does not mean that Mr. Khan was not genuinely popular. He was, especially among young men who lionized him as a sports hero. As elections loomed, a Khan wave swept Pakistan. His face was everywhere — on banners, lampposts and torn flags flying from the sputtering rickshaws that flit in and out of traffic. His supporters were the most energized and confident. His party’s symbol: a cricket bat.
Votes were still being tabulated on Thursday, but Mr. Khan’s party was far ahead, though still falling short of an outright majority in Parliament. According to results on state television, Mr. Khan’s party had 120 seats, Mr. Sharif’s party 61 and a party run by the Bhuttos, one of Pakistan’s most storied political families, 40.
Even with some vote rigging allegations unsettled, it is widely expected that Mr. Khan will entice politicians from several smaller parties to join a coalition government, with him as prime minister. Depending on how many smaller parties he woos, his government could be strong or weak.
What will Mr. Khan face?
Domestically, the challenges will be overwhelming. Pakistan’s electricity grid is disintegrated, its infant mortality rate is among the most distressing in Asia, its currency is sliding, and its debt — especially to China — is ballooning. So many Pakistanis are unable to find jobs that every year, countless young men set off on a desperate exodus to the Middle East to work as street cleaners, luggage handlers, anything.
Internationally, Pakistan is in a pinch. China has extended it billions to build roads and other infrastructure, which at the current rate will be impossible to repay.
At the same time, President Trump has cut hundreds of millions of dollars in foreign aid. “They have given us nothing but lies & deceit, thinking of our leaders as fools,” Mr. Trump said of Pakistan in a tweet in January. “They give safe haven to the terrorists we hunt in Afghanistan, with little help. No more!”
Mr. Khan deeply disagrees.
“To blame Pakistan for that disaster is extremely unfair,” he said in the recent Times interview. “The moment the U.S. went into Afghanistan, everyone knew what was going to happen.”
“It was the history of Afghanistan,” he added, citing the defeat of occupying Soviet troops in the 1980s. Just as with the Soviets, he said, the longer the Americans stayed, the more resistance would grow.
Pakistan, he says, has borne “the brunt of the war on terror,” and he lamented what he called a misguided strategy that has killed thousands of people in his country and deprived Pakistan of billions of dollars in lost business. For years, he has been a particularly vocal critic of American drone strikes.
All these positions play well with many Pakistanis. So does Mr. Khan’s recent support for the country’s strict blasphemy laws. Pakistan is socially conservative, and about 96 percent of citizens are Muslim.
Mr. Khan’s positions on religion have tacked like a sailboat over the years. But as the election drew closer, he seemed to set his course for the Islamists. In the past, he expressed support for the application of strict Islamic law, including hand amputations for thieves.
Mr. Khan has successfully rebranded himself as a populist alternative to Pakistan’s political elite, whom voters seemed more than ready to jettison.
But his life, in many ways, could not have been more different from that of most Pakistanis.
Born to a rich family in Lahore, Mr. Khan went to the best schools in Pakistan and in England, including Keble College, Oxford. He is independently wealthy, still travels abroad regularly and remains close to Ms. Goldsmith, despite their divorce.
Though many acquaintances credit him with being charming, he can also come across as remote and desultory. His attendance record as an elected member of the National Assembly, Pakistan’s Parliament, was one of the worst.
Many analysts wonder how long Mr. Khan’s friendship with the military will last.
“He is known to have erratic behavior and a very unpredictable personality,” said Taha Siddiqui, a journalist and critic of the military who recently moved to France, saying he feared for his safety.
Salman Masood contributed reporting from Islamabad, Pakistan; and Meher Ahmad and Daniyal Hassan from Lahore.