The 350-seat Congress of Deputies approved the measure Thursday by a vote of 187-147 with four abstentions. The bill, a divisive plank in the ruling Socialists' platform for social reform, gives homosexual couples the same rights as heterosexual ones.
"Matrimony shall have the same requirements and effects regardless of whether the persons involved are of the same or different sex," the bill states.
Conservative opposition lawmakers said they might challenge the law in court, while the church issued a veiled call for civil authorities who oppose gay marriage to refuse to wed same-sex couples.
The bill became law immediately. The Senate, where conservatives hold the largest number of seats, rejected it last week, but that chamber is only an advisory one and final say on legislation rests with the Congress of Deputies.
Gay couples can get married as soon as the law is published in the official government registry, which could happen as early as Friday or within two weeks at the latest, parliament's press office said.
The Netherlands and Belgium are the only other two countries that recognize gay marriage nationwide. The former allows gays to adopt children, but Belgium so far is just considering it. Canada's House of Commons passed legislation that would legalize gay marriage by the end of July as long as the Senate also passes the bill, which it is expected to do.
In the United States, Massachusetts is the only state to recognize gay marriage.
In debate before the vote, Prime Minister Jose Luis Rodriguez Zapatero said the dry language of the Spanish reform masks "an immense change in the lives of thousands of citizens. We are not legislating, ladies and gentlemen, for remote, unknown people. We are expanding opportunities for the happiness of our neighbors, our work colleagues, our friends, our relatives."
The Spanish Bishops Conference lashed out at the law, saying that with it and a bill passed Wednesday making it easier for Spaniards to divorce, "marriage, understood as the union of a man and a woman, is no longer provided for in our laws."
"It is necessary to oppose these unfair laws through all legitimate means," a conference statement said, alluding to its hint last month that town hall officials who oppose gay marriage should refuse to preside over such ceremonies.
Several members of the conservative opposition Popular Party, which vehemently opposed the bill, shouted: "This is a disgrace."
Outside the stately 19th-century chamber, activists waved the rainbow flags symbolizing the international gay rights movement.
"It is a historic day for the world's homosexuals. We have been fighting for many years," said Beatriz Gimeno, a longtime leader of the gay rights movement in Spain. "Now comes the hardest part, which is changing society's mentality."
Oscar-winning Spanish film director Pedro Almodovar, who is gay, said 21st-century families are different and don't have to reflect the traditional Catholic model.
"I don't like marriage. I am not going to get married. But it is important for this to be called marriage so people know that it is the same thing for everyone," he added.
Zapatero lacks a majority in the chamber but got more than enough help from small regional-based parties that tend to be his allies.
Popular Party leader Mariano Rajoy said Zapatero has deeply divided Spain and should have sought a consensus in parliament that recognized same-sex unions but didn't call them marriages. Rajoy said that if the vast majority of countries don't accept gay marriage, including some run by Socialists, there must be a reason.
"I think the prime minister has committed a grave act of irresponsibility," Rajoy said. He said his party is considering challenging the law before the Constitutional Court, Spain's highest tribunal.
The gay marriage bill was the boldest and most divisive initiative of the liberal social agenda Zapatero has embarked on since taking office in April 2004.
Under the divorce law reform, couples can end their marriage without a mandatory separation period or having to state a reason, as required under the old legislation. Zapatero has also pushed through legislation allowing stem-cell research and wants to loosen Spain's restrictive abortion law.
The Roman Catholic Church, which held much sway over the government just a generation ago when Gen. Francisco Franco was in power, adamantly opposed gay marriage.
In its first display of anti-government activism in 20 years, it endorsed a June 18 rally in which hundreds of thousands of people marched through Madrid in opposition to the bill. Some 20 bishops took part.
Still, polls suggest Spaniards support gay marriage. A survey released in May by pollster Instituto Opina said 62 percent of Spaniards support the government's action on gay marriage, and 30 percent oppose it. The poll had a margin of error of 3 percentage points.
But surveys also show Spaniards about evenly split over whether gay couples should be allowed to adopt children.
By Mar Roman