Peers voted down a last-minute 'fatal motion' which would have killed off gay marriage this evening, after two days of debate.
The motion by Lord Dear would have stopped the bill going to second reading and could have sparked a minor constitutional crisis, but peers voted it down by 148 to 390.
Even if the motion had been passed it would have been very unlikely to have derailed the bill, because the Commons would have used the Parliament Act to overrule the Lords.
But it would have raised serious questions about the role of the Lords, especially in the wake of the failure of Lords reform, and would have further delayed gay marriage.
Warning peers to tread carefully before pre-emptively killing off a bill which had not even gone to committee yet, Baroness Thornton said: "It is remarkable that the majority at second and third reading [in the Commons] were so large. Many MPs thought very hard."
Lord Elis-Thomas said the prospect of throwing out the bill before it had gone to committee was "like pushing the delete button before you've read the email".
Many more bishops than usual attended the debate, after Archbishop of Canterbury Justin Welby said he could not support it.
"The new marriage of the bill is an awkward shape with same gender and different gender categories scrunched into it, neither fitting well," the Archbishop said.
"The concept of marriage as a normative place for procreation is lost, the idea of marriage as covenant is diminished, the family in its normal sense. The result is confusion. Marriage is abolished, redefined and recreated."
Church officials will have been relieved their votes did not prove pivotal. Some were concerned that a vote against gay marriage which relied on the bishops could have raised questions about the rights of the established Church in the legislature.
The vote came after a long and sometimes emotional two day debate in the Lords.
Earlier today, former Conservative lord chancellor Lord Mackay informed peers that same sex couples could not procreate.
"A union between them, however loving, cannot have this purpose," he said.
"I conclude that the union open to same-sex couples in this bill is not the institution of marriage but a new and different institution which deserves a name of its own."
But former Tory frontbencher Lady Noakes told lords that homosexuality was not a lifestyle choice.
"It is as natural for them to seek lifetime relationships with a person of the same sex as it is for most of us to share our lives with an opposite-sex partner," she said.
"As a happily-married woman, I would gladly extend marriage to committed couples who happen to be of the same sex."
The bill will now go to committee. David Cameron and Nick Clegg are hoping the first marriage ceremonies will be able to take place next summer.