LONDON — Boris Johnson was so close — but still so far.

The British prime minister achieved what many said was impossible and secured a new Brexit deal with the EU, and was ready to put that deal to MPs in the House of Commons for a so-called meaningful vote.

It even looked as though he might just win that vote, and could then get on with passing the legislation required to put the deal into law with a solid mandate from parliament. But he was thwarted once again.

In the first Saturday sitting of the Commons for 37 years, MPs backed an amendment that compels the government to request a Brexit delay from the EU until all the legislation required to leave has been ratified.

Ambassadors from the 27 remaining EU countries are due to meet Sunday morning to discuss the result. EU leaders will be exasperated by what they see as yet more political instability in the U.K. They could refuse to grant an extension to negotiations but will be keen to avoid a no-deal Brexit and the uncertainty that could bring.

In a statement to the Commons following the defeat, Johnson sounded defiant. While a delay does make it much harder for the prime minister to deliver Brexit in time to meet his self-imposed deadline of delivering Brexit by October 31, defeats in parliament do play into Johnson's pitch to voters than he is trying to enact their wishes despite the efforts of those who wish to stop him.

“I will not negotiate a delay with the EU,” he said. “And neither does the law compel me to do so.”

He added: “I will tell our friends and colleagues in the EU exactly what I have told everyone in the last eight days that I have served as prime minister: that further delay will be bad for this country, bad for the European Union and bad for democracy.”

However, the British prime minister told European Council President Donald Tusk, in a phone call at 8:15 p.m. Brussels time, that he should expect a letter later on Saturday, and a few hours later Tusk confirmed he had received the letter.

"The extension request has just arrived. I will now start consulting EU leaders on how to react," Tusk said in his tweet.

Commons Speaker John Bercow had even said he would be willing to write the letter instead if he was "instructed by this House" or "directed or instructed by a court."

Johnson confirmed he would introduce the Brexit legislation to the Commons next week regardless, with government officials suggesting it would be brought forward on Monday.

Leader of the House of Commons Jacob Rees-Mogg told MPs the government would propose another motion on the deal for Monday, sparking constitutional debate over whether that would entail voting on the same motion during the same parliamentary session, which is usually against the rules.

Opposition Labour leader Jeremy Corbyn did not hide his anger. “The prime minister must now comply with the law," he said. "He can no longer use the threat of a no-deal crashout to blackmail members to support his sellout deal.”

How the votes fell

The amendment, put forward by former Tory MP Oliver Letwin, who was stripped of the whip last month for refusing to back Johnson's Brexit strategy, was passed by 322 votes to 306.

Ten former Conservative MPs, who now sit as independents after being stripped of the whip last month alongside Letwin, backed the amendment, as did most other opposition MPs.

But the final nail in the coffin for Johnson was the voting bloc of Northern Ireland's Democratic Unionist Party, his former allies in government. The DUP backed the amendment despite desperate last-ditch talks between senior figures.

Johnson angered the DUP by agreeing a deal with the EU that entails regulatory checks on goods crossing from Great Britain to Northern Ireland, effectively creating a trade border down the Irish Sea.

DUP Brexit spokesman Sammy Wilson said during the debate: “We would be failing in our duty if we do not use every strategy which is available to try and get guarantees changes and alterations which will safeguard the interests of the U.K., safeguard the interest of our constituents and safeguard the interests we represent.”

However, 12 former Tories who now sit as independents, most of whom were also stripped of the whip alongside Letwin last month, voted with the government.

One of them, Winchester MP Steve Brine, said he was “livid” about the result.

“The public will be livid too,” he told POLITICO. “Whether you think this is a good deal or a bad deal, they wanted to see their parliament today express a view on it. All that’s happened is that parliament has expressed a view yet again on what it doesn’t want. I think the public will be absolutely contemptuous of this place.”

Six Labour MPs who want Brexit done also voted with the government, including their de facto leader Caroline Flint. But a number of other Labour MPs who said they would back the Brexit deal still voted for the amendment.

A Saturday to remember

The mood in parliament for the Saturday sitting was unusual. Many MPs scurried around as though it were business as usual, although a perplexed atmosphere hung over most exchanges.

Conservative former minister Mike Penning turned up to vote in leather jacket, jeans and a rugby shirt, having come from watching the England-Australia rugby match. His dress-down approach meant he could not speak in the Commons.

Meanwhile, tens of thousands of anti-Brexit campaigners marched through central London to parliament, calling for a second referendum on EU membership. A roar went up among the crowd when the result of the vote was read out in the Commons.

Chuka Umunna, a former Labour-turned-Liberal Democrat MP who wants a second referendum, told POLITICO it was a “good day for democracy.”

“It means that whatever happens next is properly scrutinized,” he said. “In the short term, the prime minister must under the law send that letter requesting an extension. If he doesn’t do so he’ll be in contempt of court.”

An EU official, commenting on the phone call between Johnson and Tusk about the expected letter requesting an extension, said the European Council president "will on that basis start consulting EU leaders on how to react. This may take a few days."

French President Emmanuel Macron said at Friday's European Council summit that he did not think there should be a Brexit extension.

After the vote, an Elysée official said: “It is not up to us to comment at this stage [and when we will] it will be done in consultation with our European partners. But our message is clear: A deal was negotiated, it is now up to the British parliament to say whether it accepts it or rejects it. There needs to be a British vote on substance without delay because a slipping of the calendar is in the interest of no one.”

David M. Herszenhorn and Rym Momtaz contributed reporting.