The government managed to get its customs bill passed by MPs despite knife-edge votes in which a number of pro-EU Conservatives voted against their party.
Ms May suffered her biggest rebellion as prime minister as 14 Tories voted against the government. They included Guto Bebb, a defence minister, who effectively resigned his frontbench role by joining the rebels.
Ministers were forced to rely on three Labour rebels - Frank Field, Kate Hoey and Graham Stringer - voting with the government to avoid what would have been two humiliating defeats on a key Brexit bill.
It came after Ms May bowed to pressure and agreed to support four amendments tabled by Tory Brexiteers to the Taxation (Cross-border Trade) Bill.
The most contentious of these will guarantee that the UK only collects tariffs on behalf of the EU - a key part of the prime minister's Brexit strategy, as agreed by ministers at Chequers - if European countries do likewise on behalf of the UK. There is little to suggest the EU would agree to such a policy, casting major doubt over the Chequers plan just 10 days after it was agreed.
That amendment was passed with a majority of just three, as 305 MPs voted for it and 302 voted against.
A second Brexiteer amendment, which will ensure the UK has an independent VAT policy after Brexit, was also passed by just three votes.
The government also agreed to accept an amendment saying ministers must table full legislation in Parliament if they want to keep the UK in a customs union with the EU, and another that would prevent a customs border being introduced between Northern Ireland and the rest of the UK.
During a series of tight votes, MPs rejected a Labour motion that would have kept Britain in a customs union with the EU.
The late-night drama in the Commons came after a tense day of negotiations between ministers and Brexiteers, culminating in the government agreeing to support the amendments tabled by Eurosceptic Tories in the European Research Group - a bloc of 60-80 MPs led by Jacob Rees-Mogg.
These motions were tabled in a bid to scupper the government's Brexit strategy as agreed by ministers at Chequers earlier this month.
However, in an unexpected development, Downing Street insisted they would not affect Ms May's plans and that the government would therefore accept them.
Theresa May said it was "absolutely wrong" to suggest the ERG amendments were incompatible with the Chequers plan.
She told MPs: “I would not have gone through all the work that I did to ensure that we reached that agreement [at Chequers] only to see it changed in some way through these bills.
“They do not change that Chequers agreement and the minister from the despatch box later today will be making that clear.”
A Downing Street source added: “We will be accepting these four amendments because we feel they are consistent with the white paper we published last week.”
That decision prompted fury from pro-EU Tory MPs, who vowed to defy party orders and vote against the amendments.
In a heated debate, Tory MPs clashed repeatedly over the UK's future relationship with the EU.
Remainer Anna Soubry accused Ms May of “caving in” to Brexiteers and added: "Seems Jacob Rees-Mogg is running Britain."
In the Commons, she said: “The only reason that the government has accepted these amendments is because it is frightened of somewhere in the region of 40 Members of Parliament - the hard, no deal Brexiteers, who should have been seen off a long time ago and should be seen off.
“These are people who do not want a responsible Brexit, they want their version of Brexit - they don't even represent the people who actually voted Leave."
But leading Brexiteers insisted their amendments simply clarified existing government policy.
Sir Bernard Jenkin told MPs: "These amendments were only ever about clarifying government policy, that the UK will not join a customs union with the EU, nor agree to split Northern Ireland from the UK for customs purposes, that we are leaving the EU VAT regime, and any customs arrangement with the EU is not one-sided.
"By accepting these amendments, the government does no more than confirm our understanding of stated government policy."
Join our commenting forum
Join thought-provoking conversations, follow other Independent readers and see their replies