Theresa May has seen off a significant challenge to her Brexit plans, thwarting a rebel Tory move which could have forced her to try to keep Britain in a customs union with the EU.

The Prime Minister was saved from a humiliating reverse by the votes of four Labour Brexiteers – and one currently sitting as an independent – who backed the Government in the crucial division.

But 12 Conservatives broke ranks to back the customs union measure, even though it is understood that Tory whips told would-be rebels that there would be a confidence vote that evening if it passed.

Mrs May went down to defeat on a separate amendment to her flagship Trade Bill, which will require her to seek continued UK participation in the EU’s system for regulation of medicines after Brexit.

In dramatic scenes in the House of Commons, ministers made a last-ditch effort to stave off defeat by offering to introduce amendments in the House of Lords which would deal with “the essence” of rebel Tories’ concerns over future customs arrangements.

But despite the rebels’ rejection of this overture, Mrs May emerged triumphant by a margin of just six votes, as the Commons rejected the key amendment by 307-301.

Theresa May avoided defeat over a customs union by a margin of 307-301 (parliamentlive.tv)

Labour Brexiteers Frank Field, Kate Hoey, John Mann and Graham Stringer – along with independent Kelvin Hopkins – voted with the Government. All of them had previously backed the medicine regulation amendment, except for Ms Hoey who did not vote.

Former minister Guto Bebb, who quit his defence brief on Monday in protest at Government concessions to hard Brexit Tories, was among 12 Conservative MPs who rebelled over the customs union plan.

He was joined by Heidi Allen, Kenneth Clarke, Jonathan Djanogly, Dominic Grieve, Stephen Hammond, Phillip Lee, Nicky Morgan, Bob Neill, Antoinette Sandbach, Anna Soubry and Sarah Wollaston.

A senior minister told the Press Association it was “extraordinary that we lost the vote that didn’t matter and won the one that did”.

But the minister added: “I don’t know where we go from here.”

The amendment would have forced the Government to adopt a negotiating objective of seeking to keep the UK in “a customs union” with the EU after Brexit, unless it has managed to negotiate a “frictionless free trade area for goods” by January 21 next year.

Downing Street insisted that this would have breached Mrs May’s red line, set out in last year’s Lancaster House speech and enshrined in the Chequers Cabinet agreement, to take Britain out of the customs union.

But former transport minister Mr Hammond, who joined five other Tory backbenchers in tabling the amendment, insisted its provisions were “entirely in line” with Mrs May’s Brexit White Paper.

The crucial vote came moments after the Government went down to defeat by 305-301 over medicines regulation.

Some 12 Conservatives – Mr Hammond, Ms Allen, Mr Clarke, Mr Djanogly, Mr Grieve, Dr Lee, Ms Morgan, Mr Neill, Ms Sandbach, Ms Soubry, Dr Wollaston and Jeremy Lefroy – joined opposition MPs to vote that measure through.

The successful amendment requires Mrs May to make it an objective in negotiations with Brussels to ensure that the UK can continue to participate in the regulatory network operated by the European Medicines Agency.

The EMA is being relocated from London to Amsterdam as a result of Brexit.

The votes were preceded by frantic activity in the Commons, with live negotiations conducted on the floor of the chamber as Government whips led by Julian Smith made approaches to rebel MPs.

The Trade Bill later passed its third reading by a comfortable margin of 317 to 286 and will go on to the House of Lords.

International Trade Secretary Liam Fox said the legislation would be “the confident first step that the UK takes towards establishing itself as an independent trading nation for the first time in over 40 years”.

But his Labour shadow Barry Gardiner said: “The Government’s handling of Brexit over the past week has been an utter shambles. We have a Prime Minister who is in office, but not in power.

“Each day that ministers waste arguing amongst themselves increases the risk of the UK crashing out of Europe without an agreement.

“Labour is clear that a new comprehensive customs union with the EU after Brexit is the best way to protect jobs, the economy and to avoid a hard border in Northern Ireland. Theresa May should finally accept that and get on with the job of negotiating for Britain.”

Pro-EU Labour MP Chuka Umunna hit out at Labour Brexiteers who opposed the customs union amendment, saying: “It’s very disappointing and our communities will question why Labour MPs are jeopardising jobs.”

Green Party co-leader Caroline Lucas, a leading supporter of the People’s Vote campaign for a second referendum, said Brexit had “sunk further into the Westminster quagmire”.

“Parliament is deeply divided on Brexit and the Government are struggling to get anything passed,” said Ms Lucas. “The only way forward is put this issue to the public and have a People’s Vote on the final Brexit deal.”

And Liberal Democrat Brexit spokesman Tom Brake said Mrs May’s Chequers negotiating strategy was now “in tatters”.

“The passing of this deeply flawed bill can’t mask the profound splits at the heart of the Tory party,” said Mr Brake.

The Government said it would “revisit” the EMA amendment when it comes before the House of Lords.

A spokesman said: “As set out in the White Paper, the UK is seeking participation in the European Medicines Agency.

“We would look to be an active participant and this would involve making an appropriate financial contribution. We will now reflect on this amendment and seek to revisit in the Lords.”