Spain's lower house of parliament has approved the right of homosexual couples to marry and adopt children.
Gay rights campaigners cheers as the vote was read out
The government-backed bill now passes to the Senate, where it is expected to get final approval in the coming weeks.
The opposition centre-right Popular Party voted against, saying that gay relationships fall outside the traditional institution of marriage.
Religious groups, including Roman Catholic bishops, Jews and protestant bodies also expressed their opposition.
Correspondents say the law will worsen relations between the Socialist government and the Roman Catholic Church.
Socialist Prime Minister Jose Luis Rodriguez Zapatero took office in April 2004, intending to remove what he called the Church's undeniable advantages and create a secular state with streamlined divorce and relaxations in abortion law.
Under the proposed bill, Spanish Civil Law would include the phrase: "Matrimony shall have the same requisites and effects regardless of whether the persons involved are of the same or different sex."
Justice Minister Juan Fernando Lopez Aguilar argued that the bill overcomes "the barriers of discrimination, many of them with deep historical or primitive roots, which affect rights and freedoms and, in a specific way, the extension of free choice in the search for happiness, an unwritten basic right".
The vote in parliament was passed by 183 votes, with 136 against and six abstentions.
Members of gay and lesbian groups in the public gallery cheered and clapped when the result was read out.
If the bill is approved by the Senate as expected, it will make Spain the third EU country to authorise gay marriages after Belgium and the Netherlands.
The Spanish Bishops Conference, which has opposed the bill from its conception, says it goes "against the common good" and that it was "unfair that real marriage should be treated the same as the union of persons of the same sex".
Mr Zapatero, before the vote, was asked how he felt the newly elected Pope Benedict XVI might greet the news.
"If the new Pope wants to say something about it, I'm prepared to respect whatever he says, he can count on my respect for him," he said, according to the Associated Press news agency.
"One of the guarantees of democracy is the freedom of religion, freedom of opinion and freedom to carry out a political project with the citizens' vote."