On a day of high drama, the assembly deputy speaker refused to accept a motion of no confidence in the government, as Khan simultaneously appeared on TV to say there had been "foreign interference" in Pakistan's democratic institutions.
"I have sent advice to the president to dissolve the assemblies. We will go to the public and hold elections, and let the nation decide," he said.
The presidency -- a largely ceremonial office -- acceded hours later.
No Pakistan prime minister has ever completed a full term, and Khan was facing the biggest challenge to his rule since being elected in 2018, with opponents accusing him of economic mismanagement and bungling foreign policy.
On Sunday parliament was due to debate a no-confidence motion that looked certain to succeed, but the deputy speaker -- a Khan loyalist -- refused to accept it on the grounds it had a "clear nexus" with a foreign state to bring about a change of government.
It caused uproar in the chamber, but there was calm on the streets on the first day of the fasting month of Ramadan, with a huge security presence in the capital.
Khan, an ex-international cricket star who in 1992 captained Pakistan to their only World Cup win, had hinted Saturday he still had a card to play -- and Sunday's move appeared to blindside the opposition.
"Astonished by the reaction," he tweeted later, adding the opposition had been "crying hoarse" about the government failing and losing the support of the people.
"So why the fear of elections now?"
- 'Black day' for Pakistan -
"This day will be remembered as a black day in Pakistan's constitutional history," said Shehbaz Sharif, tipped to replace Khan if the no-confidence motion had succeeded.
The opposition and government immediately filed a slew of petitions and briefs against and for the developments, with the supreme court saying arguments would be heard on Monday.
"It is of utmost importance that political parties and state functionaries act accordance to law and refrain from taking unconstitutional steps," said Chief Justice Umar Ata Bandial.
Khan's Pakistan Tehreek-e-Insaf party (PTI) effectively lost its majority in the 342-member assembly last week when a coalition partner said its seven lawmakers would vote with the opposition.
More than a dozen PTI members had also indicated they would cross the floor.
In days leading up to the vote, Khan accused the opposition of conspiring with "foreign powers" to remove him because he would not take the West's side on global issues against Russia and China.
On Thursday he accused the United States of meddling in Pakistan's affairs.
Local media had reported that Khan received a briefing letter from Islamabad's ambassador to Washington recording a senior US official saying they felt relations would be better if Khan left office.
In Washington last week State Department spokesman Ned Price told reporters there was "no truth" to the allegations, but Khan insisted Sunday it was "regime change" and accused the opposition of betraying the country.
"This betrayal was taking place in front of the entire nation... traitors were sitting and planning this conspiracy," he added.
- Opposition gathers -
The opposition is headed by the Pakistan Muslim League-N (PML-N) and the Pakistan Peoples Party (PPP) -- two usually feuding dynastic groups that dominated national politics for decades until Khan forged a coalition against them.
Khan was elected after promising to sweep away decades of entrenched corruption and cronyism, but has struggled to maintain support with inflation skyrocketing, a feeble rupee and crippling debt.
Some analysts said Khan had also lost the crucial support of the military -- claims both sides deny -- but it is unlikely he would have pulled off Sunday's manoeuvre without its knowledge, if not blessing.
There have been four military coups -- and at least as many unsuccessful ones -- since independence in 1947, and the country has spent more than three decades under army rule.
"The best option in this situation is fresh elections to enable the new government to handle economic, political, and external problems faced by the country," said Talat Masood, a general turned political analyst.