The government's gay marriage bill was saved after David Cameron was forced to rely on Ed Miliband to defeat an attempt by his own MPs to derail the measure by trying to extend civil partnerships to heterosexual couples.

An 11th-hour plea to the Labour leadership by the Tory chief whip Sir George Young, who warned that the government was in danger of losing the vote, prompted a change of heart by Miliband, who had been planning to abstain on the amendment.

The Labour move meant that the amendment, tabled by the anti-gay marriage Tory and former children's minister Tim Loughton, was defeated by 375 to 70 votes, a majority of 305.

The decision by the Labour leadership, which has gone from supporting the amendment on civil partnerships to rejecting it within the space of 24 hours, means that the marriage (same-sex couples) bill will now experience a safer journey through parliament.

But the prime minister, who attempted to reach out to his party by emailing a "personal note" to all members saying that he would never work with anyone who "sneered" at them, suffered the humiliation of having to plead with the Labour party for support. He also saw more than 100 Tory MPs, including the cabinet ministers Iain Duncan Smith and Owen Paterson, vote against him on the first amendment of the day.

The prime minister will understand the dangers of relying on opposition support for a flagship measure after he personally ensured that Tony Blair's schools reforms survived with Tory support in 2006 three months after he became leader. Within months, supporters of Gordon Brown forced Blair to name the date of his departure the following year.

As the debate was under way in the Commons the prime minister moved to shore up his position amid anger in the party over allegations that Lord Feldman, the Tory co-chairman, described grassroots activists as "mad swivel-eyed loons". Lord Feldman strenuously denies having made the allegations.

In his email to party members, Cameron wrote: "I am proud to lead this party. I am proud of what you do. And I would never have around me those who sneered or thought otherwise. We are a team, from the parish council to the local association to parliament, and I never forget it."

But deep divisions in the Tory party were highlighted in the commons when Iain Duncan Smith, the work and pensions secretary, and his long standing ally Owen Paterson, the environment secretary, joined more than 100 Tory MPs to vote against Cameron in favour of an amendment that would allow registrars to opt out of conducting same sex marriage ceremonies. This amendment failed as did an amendment to protect the religious beliefs of a person who believes marriage can only take place between a man and women. All votes were classified as free which meant that MPs could vote according to their consciences.

In one of the most dramatic moments the former defence minister, Sir Gerald Howarth, complained to a lesbian member of the prime minister's policy board about "the aggressive homosexual community". Howarth made the remarks after Margot James, the MP for Stourbridge, said that the legislation was part of recent changes that have created a level playing fields for everyone regardless of sexual orientation.

The prime minister came under fire from the anti-gay marriage MP Tim Loughton after his amendment, which would have legalised civil partnerships for heterosexual couples, failed after the deal between Labour and the Tories. Loughton warned of a "grubby deal" between the two frontbenches as he told MPs: "We are in danger to a stitch up, a last minute stitch up between frontbenches."

The deal was reached after the government had warned earlier in the day that the Loughton amendment would have threatened the entire bill by adding £4bn to the costs and delaying its implementation. The costs would have come from increased pension survivor rates for new civil partners.

The government agreed during the day to a Labour request to amend its own plans by launching an immediate review into extending civil partnerships to heterosexual couples. The goverment had initially said it would do this no later than five years after the passage of the bill, though the equalities minister Maria Miller said the Labour amendment would make little practical difference.

The deal meant that the government amendment, altered by Labour, was approved by 391 to 57 votes, a majority of 334.

But Miller indicated that the review could end up leading to the abolition of civil partnerships once gay marriage becomes legal. She told MPs of the review: "It is important for us to understand what the demand is among individuals who might wish to embark on such an arrangement."

Labour sources said that the party, which had announced earlier in the day that it would abstain on the Loughton amendment after overnight warnings from the government about the threat to bill, denied that Miliband had embarked on a double U-turn.

One source said: "We had an eleventh hour appeal from the government that they did not have the numbers to defeat the Tim Loughton amendment. They made repeated approaches to us at ever increasing levels.

"Ed's overriding priority is to ensure that the bill gets on to the statute book. Ed and Yvette Cooper will therefore be voting against the Tim Loughton amendment. We expect a large number of MPs to join Ed and Yvette. Since there was a genuine threat to the bill Ed decided the best thing to do was to act in this way."

The leaders of all the main parties offered all their MPs, including ministers and shadow ministers, a free vote on the grounds that marriage is a "conscience" social issue in which the party whips have no official say. But the prime minister devoted government time to the gay marriage legislation in the belief that it would help reach out to centre ground voters who may feel uncomfortable about supporting a party whose leader voted in favour of the retention of section 28 as recently as 10 years ago. A source close to Miller said: "We are pleased that the House has accepted our amendment offering a review of civil partnerships and that our warnings around the potential delay to same sex marriage have been heeded. A review is the right way forward and no changes should be made to civil partnerships, without being fully thought through." Tory supporters of the bill were scathing about some of their fellow MPs. One said: "You know how to vote when you see who's in the other division lobby."

One minister said: "We are such an inclusive party we have our own opposition built in. We generally shoot ourselves in the foot and then rely on the Labour party to finish the job for us. And all the time we seem to have a smile on our face."