Spain's lower house of parliament has voted in favour of allowing gay couples to marry and adopt children.
The law puts same-sex couples on the same footing as heterosexuals
The controversial decision overrules last week's rejection of the bill by the upper house, the Senate.
The bill will become law in a month's time, making Spain Europe's third nation after the Netherlands and Belgium to allow same sex marriages.
Polls suggest most Spaniards back the move, although two weeks ago thousands joined a Madrid rally against the bill.
Need for unity
A Roman Catholic group had presented MPs with a 600,000-signature petition opposing the legislation and were lobbying hard for a referendum on the issue.
They are urging conservative lawmakers to take legal action to have gay marriages declared unconstitutional.
Some of Spain's local mayors have said they will not officiate at gay marriages.
The Roman Catholic Church wields great influence in Spain, but the BBC's Katya Adler in Madrid says that since the Madrid bomb attacks in March 2004, Spaniards want unity among their people more than anything.
The vote was carried with 187 votes in favour, 147 against and four abstentions.
Couples lining up
The new legislation is one of a series of social reforms, including faster divorce proceedings, being introduced by Spain's socialist government, led by Prime Minister Jose Luis Rodriguez Zapatero.
Addressing MPs before the vote Mr Zapatero made an appeal for the law to be passed:
"We were not the first, but I am sure we will not be the last. After us will come many other countries, driven, ladies and gentlemen, by two unstoppable forces: freedom and equality," he said.
"We are not legislating, ladies and gentlemen, for remote unknown people. We are expanding opportunities for the happiness of our neighbours, our work colleagues, our friends, our relatives."
There were cheers outside parliament following the vote.
The new law puts same-sex and heterosexual marriages on the same legal footing, including the right to adopt children.
About 5,000 gay couples in Spain have already announced they are queuing up to say "I do", our correspondent says.