Mexico City has become the first city in Latin America to legalise same-sex marriage, giving gay couples more rights, including allowing them to adopt children.

The bill passed the capital's local assembly by 39 votes to 20 yesterday as supporters chanted: "Yes, we could! Yes, we could!"

The city's leftwing mayor, Marcelo Ebrard, of the Democratic Revolution party, had been widely expected to sign the measure into law.

The assembly has made several decisions that have been unpopular elsewhere in the deeply Roman Catholic country, including legalising abortion in the first 12 weeks of pregnancy.

That sparked a backlash, with the majority of Mexico's other 32 states enacting legislation declaring that life begins at conception.

The conservative National Action party, led by the Mexican president, Felipe Calderón, vowed to challenge the new gay marriage law in the courts.

However, homosexuality is increasingly accepted in Mexico, with gay couples holding hands in parts of the capital and the annual gay pride parade attracting tens of thousands of people.

The bill called for a change of the definition of marriage in the city's civil code.

It is currently defined as the union of a man and a woman, and the new definition will be "the free uniting of two people".

The change will enable same-sex couples to adopt, apply for bank loans, inherit wealth and be included in the insurance policies of their spouse – rights they were denied under the civil unions allowed in the city.

"We are so happy," said Temistocles Villanueva, a 23-year-old film student, who celebrated the new legislation by kissing his boyfriend outside the city assembly.

The Argentinian capital, Buenos Aires, became the first Latin American city to legalise same-sex civil unions for gay and lesbian couples in 2002.

Four other Argentine cities later did the same, as did Mexico City in 2007 and some Mexican and Brazilian states. Only Uruguay has legalised civil unions nationwide.

Buenos Aires officials introduced a bill for legalising gay marriage in the national congress in October, but it stalled without a vote and officials blocked same-sex weddings because of conflicting judicial rulings.

Many people in Mexico and elsewhere in Latin America remain opposed to gay marriage, and the Roman Catholic church has announced its opposition.