Credit…Saiyna Bashir for The New York Times

ISLAMABAD, Pakistan — Pakistan, a nuclear-armed nation that is home to the world’s second-largest Muslim population, has for decades been a reluctant — if important — American partner in the campaign against terrorism.

But the country drifted away from the United States under Prime Minister Imran Khan, particularly after the American withdrawal from Afghanistan, where Pakistan was long accused of nurturing the Taliban and is a supporter of the Taliban regime that took over last year. Pakistan has also embraced a strategic partnership with China and closer ties with Russia.

For two decades after the Sept. 11, 2001, attacks, Pakistan was ostensibly a U.S. partner in the war against Al Qaeda and the Taliban after the United States demanded that Pakistan choose sides. In exchange, Pakistan’s military won tens of billions of dollars in American aid.

But from the start, the relationship between the two countries was rife with divided interests, with Pakistan playing a double game: accepting American aid, while often backing the very militants that the United States was fighting.

The Pakistani spy agency provided planning assistance and training expertise to the Taliban throughout the Afghan war, American officials have said, and offered a haven to the Haqqani network, a militant organization responsible for some of the deadliest attacks against American troops in Afghanistan. After the Taliban seized power, Pakistani protégés in the Haqqani network took on key positions in the Afghan government.

Pakistan’s goal in Afghanistan was to create a sphere of influence to block its archnemesis, India, which, according to Pakistan, supports separatist groups operating from havens in Afghanistan to stir unrest in Pakistan.

During the Afghan war, the United States tolerated Pakistan’s duplicitous game because given the choice, American officials preferred fighting a chaotic war in Afghanistan to falling out with a nuclear-armed Pakistan. Pakistan’s ports and airfields offered critical entry points and supply lines for American military equipment needed in Afghanistan.

But the U.S. relationship with Pakistan cooled after Navy SEALs killed Osama bin Laden in 2011 at a safe house located near a Pakistani military academy.

China, a longtime patron of Pakistan, has invested heavily in Pakistani infrastructure. China is also counting on Pakistan to serve as its facilitator in Afghanistan, home to millions of dollars’ worth of rare earth minerals that have piqued China’s interests, analysts say. Mr. Khan, in trying to establish closer ties with Moscow, also visited President Vladimir V. Putin in Russia hours before the invasion of Ukraine. Mr. Khan intended to push for a multibillion-dollar gas pipeline to be built by Pakistani and Russian companies, according to news reports in Pakistan.

But if Mr. Khan is ousted, many experts on the region say that Pakistan could grow closer to the United States and the West. Over the past three years, Pakistan’s military, which has historically determined the country’s foreign policy and security priorities, has often disagreed with Mr. Khan’s views on distancing from the United States, analysts say. Those differences contributed to Mr. Khan’s relationship with the military souring in the latter part of his tenure.

The day before the no-confidence vote was planned to take place, the chief of the Pakistani Army, Gen. Qamar Javed Bajwa, voiced a desire to deepen ties with both China and the United States, and condoned Russia’s invasion of Ukraine.