The supreme court still has to rule on the adoption clause and whether the ruling will affect states outside the capital. It is expected to address adoption on Monday.
Jaime Lopez Vela, a leader of the group Lesbian, Gay, Transsexual and Transgender, said "now we hope that the final ruling declares it all constitutional", referring to adoption.
He was among a group of activists celebrating the ruling outside the court.
Leticia Bonifaz, a Mexico City lawyer who argued Mexico City's case, said: "We are very happy. It fell to us to carry to a conclusion a struggle that has taken a long time."
"It does not appear to me to be unconstitutional. The concept of the family established in the constitution ... is an open concept"
Justice Jose Gudino
Justices who voted to uphold the law said that while Mexico's constitution enshrines protection for families, it does not define what a "family" is.
"It does not appear to me to be unconstitutional," Justice Jose Gudino said during Thursday's session.
"The concept of the family established in the constitution ... is an open concept."
Mexico's Roman Catholic church and the conservative government of Felipe Calderon, the Mexican president, had opposed the law.
Reverend Hugo Valdemar, the spokesman for Mexico City's Roman Catholic Archdiocese, said: "We regret this ruling because in our opinion, it affects the fundamental nucleus of the family."
The president of a local Catholic lawyers' group said his organisation would be even more concerned if the court upholds the part of the Mexico City law that lets same-sex couples adopt children.
"That would directly affect the rights of children," Armando Martinez said. "We will seek impeachment hearings against any justices that vote in favor of adoption."
Federal prosecutors had cited an article in Mexico's constitution that suggests - but does not state - that families are constituted by men, women and children.
The article states: "Men and women are equal before the law. This protects the organisation and development of the family."
Pros and cons
Justice Guillermo Ortiz, who argued against the law, said "marriage is reserved exclusively for couples who can procreate, because one of the big issues of marriage is the protection of children".
But another judge, Jose Fernando Franco, argued that "procreation is not an essential element of marriage".
"Those who wish to procreate are free to do so, not only within marriage but in any way they see best, and this happens and can happen in heterosexual marriages, and those that are not, or among single persons," Franco said.
The justices who voted to uphold the law differed in their reasons why: Some stressed the constitution's protection of an individual's right to choose a marriage partner, and others the right of local legislatures to enact laws governing the issue.
Justice Luis Aguilar Morales argued against framing the ruling around the individual rights issue, something that might force other states to adopt similar measures.
"If Mexico City wants it a certain way, that does not necessarily mean that the rest of the states have to do the same,'' Aguilar Morales said.
The issue will apparently be worked out in subsequent discussion and the writing of the final ruling.
City authorities said that as of earlier this week, 320 couples had been married under the law: 173 weddings between men and 147 between women.