Argentina has become the first country in Latin America to legalise gay marriage after the Senate voted in favour.The country's Chamber of Deputies has already approved the legislation.
The vote in the Senate, which backed the bill by just six votes, came after 14 hours of at times heated debate.
The law, which also allows same-sex couples to adopt, had met with fierce opposition from the Catholic Church and other religious groups.
Thousands protested outside Congress as debates took place inside.
At Sunday Mass, people were urged to protest on the eve of the vote by senators on whether to legalise gay marriage in the country.
"I came to defend marriage between man and woman, because I think any attempt against this is an attempt against natural law," said Angeles Navarro, one of the protesters.
The main slogan for the protest against the legislation was "the children have a right to a mother and a father".
There were demonstrations for and against the bill outside Congress as senators debated . Small groups of demonstrators gathered in support of the bill.
There has been a number of gay marriages recently in Argentina, some of which were annulled by the Supreme Court, creating a legal controversy.
"Same-sex families already exist, gay families already exist, so the task that they (the senate) will have... is to legalise the situation of all those families," said one protester.
Civil unions between people of the same sex are legal in the city of Buenos Aires and in some other provinces but there was no law to regulates it on a country-wide level.
President Cristina Fernandez had stated that she would not veto the measure if it arrived in her office.
The city of Buenos Aires, Argentina's capital, is widely considered to be among the most gay-friendly cities in Latin America. It was the first Latin American city to legalise same-sex unions.