Theresa May. File photo
In a night of high drama in the Commons, MPs surprised the government and voted by 312 to 308 to reject a no-deal Brexit under any circumstances.
The vote is not binding - under current law, the UK could still leave without a deal on 29 March.
On Thursday, MPs will vote on whether to ask the EU for permission to delay the date for departure.
There could be a short extension - or a much longer one - depending on whether MPs backed the prime minister's existing withdrawal deal that has been agreed with the EU by 20 March, the government says.
That means Theresa May could make a third attempt to get her deal through Parliament in the next few days. In a series of votes on no-deal Brexit, the Commons first voted by a margin of four to reject no deal outright.
Then, in a second vote, they reinforced that decision by 321 to 278, a majority of 43.
That second vote was on a government motion, which said the UK should not leave the EU without a deal on 29 March, but with the option of a no-deal Brexit at any other time.
The government wanted to keep control of the Brexit process, and keep no-deal on the table, so they ordered Conservative MPs to vote against their own motion.
That tactic failed. Government ministers defied those orders and there were claims Theresa May had lost control of her party.
Thirteen government ministers - including Work and Pensions Secretary Amber Rudd, Business Secretary Greg Clark, Justice Secretary David Gauke and Scottish Secretary David Mundell - defied the government whips by abstaining in the vote.
Work and pensions minister Sarah Newton voted against the orders of the whips and has now resigned.
Mr Mundell said he backed the PM's deal and had always made clear his opposition to a no-deal Brexit.