The government has survived an attempt by pro-EU Conservative MPs to change its post-Brexit trade strategy.
The MPs wanted the UK to join a customs union if it does not agree a free-trade deal with the EU.
But the government, which says a customs union would stop it striking new trade deals, won by 307 to 301.
Ahead of the vote, Tory MPs were told a defeat would lead to a vote of no confidence in the government, sources told the BBC's John Pienaar.
The government did, however, lose a separate vote on its Trade Bill on the regulation of medicines after Brexit.
MPs backed an amendment by 305 votes to 301 that would keep the UK in the European medicines regulatory network.
There were 12 Tory rebels in both the customs and medicines votes - but the government's total was boosted by four Labour MPs in the customs vote.
The customs union allows for tariff-free trading between members with a common tariff set for imports from the rest of the world.
The UK is due to leave the EU on 29 March 2019 but the two sides have yet to agree how their final trading relationship will work.
The Commons has been debating two pieces of legislation - on customs and trade - and there have been several attempts to change them by both pro-Brexit and pro-EU MPs.
The latest key vote was on customs, with a debate sparked by Tory MP Stephen Hammond's amendment to the Trade Bill.
It stated that if a free trade area had not been negotiated by 21 January, ministers must change tack and start discussions on joining a customs union.
Labour backs the idea of a customs union with the EU after Brexit - but the government says this would mean the UK is unable to strike its own international trade deals.
During the debate on the Trade Bill, a minister tried to persuade Mr Hammond and his supporters to back down, promising to deal with the "essence" of their concerns when the bill goes to the House of Lords.
Although this was rejected by Mr Hammond, the government won the vote and the bill was later approved by the House of Commons.
The 12 MPs that voted against the government on customs and trade were: Heidi Allan, Guto Bebb, Ken Clarke, Jonathan Djanogly, Dominic Grieve, Stephen Hammond, Philip Lee, Nicky Morgan, Bob Neill, Antoinette Sandbach, Anna Soubry and Sarah Wollaston.
On the other side, four Labour MPs voted with the government: Frank Field, Kate Hoey, John Mann, and Graham Stringer.
Lib Dem leader Sir Vince Cable and his predecessor Tim Farron - who were criticised after missing Monday night's knife-edge Brexit votes - were back at Westminster and voted against the government.
But a former Lib Dem minister, Jo Swinson, complained that Tory MP Brandon Lewis participated in the votes despite having agreed to abstain.
Ms Swinson, a Remain supporter who has recently given birth and was unable to vote, tweeted Mr Lewis did not keep to their arrangement to balance out her effective abstention - known as "pairing" - and accused the government of "desperate stuff".
Laura Kuenssberg's analysis
They didn't escape defeat for long.
Having squeaked through last night the government was beaten for only the second time ever in the Commons on key Brexit legislation.
And guess what, it was on a vote they didn't expect to lose - Dr Phillip Lee, who quit the government to speak out on Brexit, put forward his own amendment to protect the links between the UK and the European Medicines Agency to ensure the smooth flow of medicines and new drugs for British patients after Brexit.
And he had enough to support to win it, just. An embarrassment for the government certainly.
It is another reminder of how difficult it is for Theresa May to get her way in the House of Commons where she doesn't have her own majority.
It is serious. A defeat is a problem. But it wasn't a complete disaster tonight for two reasons.
First, the amendment isn't a million miles away from the government's own policy. While not straightforward, the vote hasn't forced a screeching U-turn.
The more important reason is that the vote that followed, on keeping the UK in a customs union, went the other way.
The medicines vote explained
The vote on medicine regulation was only the government's second defeat on Brexit in the House of Commons.
MPs voted for the UK to take "all necessary steps" to participate in the regulatory network operated by the European Medicines Agency after it leaves the EU.
The agency, which evaluates and supervises medicines and helps national authorities authorise the sale of drugs across the EU, is currently based in London but is moving to Amsterdam after Brexit.
There have been warnings that Brexit may cause delays in UK patients getting new drugs.
The government says it is "seeking participation" in the agency after Brexit and would make an "appropriate financial contribution" in return.
But it has not agreed to take "all necessary steps" to secure this.
Responding to the defeat, the government said: "We will now reflect on this amendment and seek to revisit in the Lords."