ISLAMABAD, Pakistan—As a sportsman, Imran Khan led an underdog Pakistan team to victory at cricket’s World Cup. Now he is drawing on that competitive spirit as he tries to come from behind to win a vote that will determine his fate as prime minister.

Mr. Khan faces a no-confidence vote in parliament, which is expected to take place by April 4 and will decide whether he stays in office.

The numbers are heavily stacked against the 69-year-old leader’s thin majority in parliament. At least a dozen lawmakers from his own party have said they are deserting him, and other parties in his ruling coalition have signaled they are ready to switch sides. And Pakistan’s powerful military, which aided his rise to power, has turned cold on him.

He hopes to demonstrate his support with a massive rally in the capital on Sunday. Some of those who have met with him in recent days say he has told them he won’t resign.

Mr. Khan is challenging the vote against him in the courts, arguing that lawmakers aren’t allowed to turn on their party. He is also trying to convince opposition lawmakers to come over to his side. Mr. Khan says the opposition’s attempt to oust him has galvanized his supporters. He could call for early elections.

“I’m seeing an Imran Khan I haven’t seen for 15, 20 years. He’s at his best when he’s confronted with a huge challenge,” said an aide. “He’s relishing it.”

Imran Khan raises his arms in triumph during Pakistan’s victory over England in the 1992 Cricket World Cup final.

Photo: Joe Mann /Allsport/Getty Images

Mr. Khan was a charismatic hero for Pakistanis on the cricket pitch, but he has proven deeply polarizing as a politician. His politics have become increasingly cloaked in his religious beliefs. He says his aim is to establish an Islamic welfare state, casting his opponents as not only crooked but evil. His third—and current—marriage was to his spiritual guide.

“God tells Muslims to stand up for goodness and go against evil,” Mr. Khan said in a video message to supporters this week. “A gang of thieves, who has been looting this country for the past 30 years, doing corruption and sending the proceeds abroad, has united.”

Mr. Khan has a base of passionate supporters, but swing voters have moved toward his main rival, three-time prime minister Nawaz Sharif, who was 19 points ahead in popularity in a poll conducted in January by Gallup Pakistan.

Government employees in Quetta, Pakistan, this month protested against rising inflation.

Photo: jamal taraqai/Shutterstock

The biggest issue for voters is inflation, the poll showed, which has plagued Mr. Khan’s tenure, driven by more-expensive imports as the rupee has plummeted. An official price index, published on March 24, of 51 essential items, including food and fuel, showed annual inflation running at 16%.

Foreign debt has ballooned, and the continuation of an International Monetary Fund bailout program is in doubt because the government adopted fuel subsidies and a tax amnesty before getting IMF approval.

Mr. Khan’s party says it should get credit for its handling of the Covid-19 pandemic, which brought fewer deaths and less economic damage to Pakistan than neighboring India and many other countries. The government has bolstered cash handouts to the poor and allowed people to use private hospitals at government expense. It also says the economy is beginning to turn around.

As prime minister, Mr. Khan brought corruption cases against dozens of his opponents, though none of those charges resulted in a conviction. He calls them rats and refuses to shake hands with them. That aggressive style, and what detractors say is hypocrisy as he surrounds himself with many familiar faces in Pakistani politics while professing a new approach, mean that he is adored and reviled in equal measure.

The prime minister’s party has claimed credit for successes in Pakistan’s pandemic response.

Photo: nadeem khawar/Shutterstock

Mr. Khan started playing for the national cricket team as a teenager, going on to be captain and one of its greatest ever players. He is also a successful philanthropist, raising funds to establish a cancer hospital and a university.

But his path in politics, which followed his retirement from cricket, was much more difficult. He spent 22 years in the opposition as he attempted to break Pakistan’s two-party system as a self-styled Mr. Clean in one of the world’s most corrupt countries.

As the 2018 election approached, Mr. Khan was in his mid-60s and could see his chance at power slipping away, his aides say. At that point, he made two key compromises to get to power. First, he took in a number of politicians who had previously hopped from party to party—the kind he had railed against for years as being corrupt. Some of those politicians have now turned on him.

The second thing he did, opponents say, was deciding to cooperate with the military, which helped shore up his support in the crucial electoral battleground of Punjab province, home to half the country’s population. He and the military deny that they worked together to help him win.

The current challenge against him is possible only because the military has remained neutral, opposition lawmakers say, effectively abandoning Mr. Khan and freeing his supporters in parliament to jump ship. The military, which denies interfering in politics, has staged multiple coups and, even when not formally in power, it asserts a dominant role behind the scenes in policy-making.

Imran Khan, pictured third from left with Pakistan’s president and top military officials, has had to navigate tensions with leaders of the armed forces.

Photo: Anjum Naveed/Associated Press

Mr. Khan clashed with the armed forces over key appointments and the performance of his government. His aides say that the military leadership also grew uncomfortable with his increasingly anti-Western politics, taking the country into closer alignment with China and, more recently, Russia. When the Taliban overthrew the U.S.-backed government in Kabul, he said that Afghanistan had “broken the shackles of slavery.” He visited President Vladimir Putin at the Kremlin on the day that Russia invaded Ukraine last month. The military, dependent on American equipment from decades of alliance with Washington, doesn’t want to alienate the U.S. and other Western powers.

“We are closely following developments in Pakistan,” said a U.S. State Department spokeswoman. “We respect and support Pakistan’s constitutional process and the rule of law.” President Biden hasn’t called Mr. Khan since taking office, which is seen as a snub in Pakistan.

Mr. Khan is widely believed to retain support among the rank and file of the army, meaning the move against him isn’t without risk for the armed forces.

Opposition politicians say they are pouncing now because they fear Mr. Khan could choose an army chief later this year who will again skew the election in his favor.

Pakistan has never had a prime minister complete a full five-year term. Mr. Khan’s is due to end in mid-2023. The tensions between Mr. Khan’s government and the military follow a pattern that has played out in Pakistani politics for decades.

“After a couple of years, a prime minister tries to assert his or her authority, a difference of opinion arises, and then it goes to a breaking point,” said Ahmed Bilal Mehboob, president of the Pakistan Institute of Legislative Development and Transparency, a think tank. “This is something we have seen with almost perfect regularity.”

Write to Saeed Shah at saeed.shah@wsj.com