Image copyright AFP Image caption Spaniard Manuel Valero told reporters outside the court in Bangkok that he was delighted the nightmare of the custody battle was over

A same-sex American-Spanish couple have won a high-profile custody battle over a baby born through a Thai surrogate mother who wanted to keep the child after she found out they were gay.

Bangkok's Family Court ruled the legal guardian of the child is Gordon Lake, her biological father from the US.

The child was born before a Thai ban on commercial surrogacy was imposed.

Correspondents say that the case was complicated by the fact that Thai law does not recognise same-sex marriages.

"The court has granted legal custody of [the child] Carmen Lake to Gordon Lake, my client, and [said] that my client is her only guardian," Mr Lake's lawyer Rachapol Sirikulchit said.

Image copyright AFP

Mr Lake and his partner, Spaniard Manuel Santos, both 41, have been in Thailand since beginning their legal battle over Carmen since she was born in January 2015.

Thailand's crackdown on 'wombs for rent'

Thailand bans foreigner surrogacy

Mr Santos emerged from the court smiling and tearful.

"We won," he told journalists. "We are really happy... This nightmare is going to end soon."

He said Carmen would now fly to Spain, where the couple live.

When the child was born, Thai surrogate Patidta Kusolsang handed her over to Mr Lake and Mr Santos, who left the hospital with her in their arms.

But they say Ms Kusolsang subsequently changed her mind and refused to sign documents allowing Carmen to get a passport so that she could leave Thailand.

While Mr Lake is Carmen's biological father, the egg came from an anonymous donor - not the surrogate mother.

Mr Lake and Mr Santos were informed that Ms Kusolsang had assumed the child was being looked after by an "ordinary family" and that she worried about the child's upbringing.

Mr Lake began a crowdfunding website that has raised about $36,000 to help cover the costs of the trial and staying in Thailand.

Thailand in February 2015 banned foreigners from paying Thai women to be surrogates, after two high-profile cases sparked debate in 2014.

The legislation also bans the use of agents, or any promotion of women willing to carry babies for others.

Under the new law, only married Thai couples or couples with one Thai partner who have been married at least three years can seek surrogacy, and commercial surrogacy is banned.

Anyone caught hiring a surrogate mother faces a maximum jail sentence of 10 years.

Agents touting surrogate mothers would also face imprisonment if caught.

Surrogacy is unlawful in Spain.