Jubilant gay activists blew kisses to lawmakers after the vote.
The measure passed the 350-seat Congress of Deputies by a vote of 187 to 147. The bill, part of the ruling Socialists' aggressive agenda for social reform, also lets gay couples adopt children and inherit each others' property.
The bill is now law. The Senate, where conservatives hold the largest number of seats, rejected the bill last week. But it is an advisory body and final say on legislation rests with the Congress of Deputies.
After the final tally was announced, gay and lesbian activists watching from the spectator section of the ornate chamber cried, cheered, hugged, waved to lawmakers and blew them kisses.
Several members of the conservative opposition Popular Party, which was vehemently opposed to the bill, shouted: "This is a disgrace." Those in favor stood and clapped.
The Netherlands and Belgium are the only other two countries that allow gay marriage nationwide. Canada's House of Commons passed legislation Tuesday that would legalize gay marriage; its Senate is expected to pass the bill into law by the end of July.
Prime Minister Jose Luis Rodriguez Zapatero noted this in debate before the vote.
"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 told the chamber.
Zapatero said the reform of Spanish legal code simply adds one dry paragraph of legalese but means much more.
He called it "a small change in wording that means 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."
Zapatero lacks a majority in the chamber but got help from small regional-based parties that tend to be his allies.
Spanish gay couples can get married as soon as the law is published in the official government registry. This could come as early as Friday, or within two weeks at the latest, parliament's press
Popular Party leader Mariano Rajoy said after the vote that Zapatero has deeply divided Spain and should have sought a consensus in parliament that recognized same-sex unions but didn't call them marriage. Rajor said that if the vast majority of countries in the world 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," Rajor told reporters.
Beatriz Gimeno, a longtime leader of the gay rights movement in Spain, held back tears as she hugged her partner Boti after the vote.
"It is a historic day for the world's homosexuals. We have been fighting for many years," Gimeno said. "Now comes the hardest part, which is changing society's mentality."
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. Parliament overhauled Spain's 25-year-old divorce law on Wednesday, also irking the church, by letting couples end their marriage without a mandatory separation or having to state a reason for the split-up, as required under the old law.
He 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, had 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 marched through Madrid in opposition to the bill. Some 20 bishops took part in the June 18 rally.
On Wednesday, a Catholic lay group called the Spanish Family Forum presented lawmakers with a petition bearing 600,000 signatures as a last-minute protest.
Late last year, the spokesman for the Spanish Bishops Conference, Antonio Martinez Camino said that allowing gay marriage was like "imposing a virus on society _ something false that will have negative consequences for social life."
Despite the street protests in Madrid and elsewhere and the petition drive, polls suggest Spaniards supported gay marriage.
A survey released in May by pollster Instituto Opina said 62 percent of Spaniards support the government's action on this issue, and 30 percent oppose it. The poll had a margin of error of 3 percentage points.
But surveys show Spaniards about evenly split over whether gay couples should be allowed to adopt children.