GENEVA (Reuters) - The U.N. Human Rights Council agreed on Thursday to appoint an independent investigator to help protect homosexuals and transgender people worldwide from violence and discrimination.

A participant holds up a rainbow flag during a gay pride parade in Managua, Nicaragua June 28, 2016. REUTERS/Oswaldo Rivas

After a heated debate lasting almost four hours, the 47-member state forum overcame strong objections by Saudi Arabia and Muslim countries to adopt a Western-backed resolution by a vote of 23 states in favor and 18 against with six abstentions.

The United Nations expert, still to be named, will have a three-year mandate.

Mexico, which led Latin American states that were the main sponsors of the text, said that thousands of people are exposed to violence and discrimination due to their sexual orientation and gender identity.

“Remember Orlando,” Mexican Ambassador Jorge Lomónaco told delegates, referring to the massacre of 49 people at a gay club in Florida on June 12. “Let us give hope to millions.”

The United States and major European countries backed the resolution, while China, Russia and 16 African and predominantly Muslim states rejected it. India, South Africa and the Philippines were among the abstainers.

“This Council regularly – and rightly – passes resolutions on racism, women and children. Yet, on this issue, we often hear of culture and tradition as reasons to justify violence against individuals based on their sexual orientation and gender identity,” British Ambassador Julian Braithwaite said in debate.

“This affects people in this room, and people in my team who are LGBT. Are you saying it is okay to discriminate against them based on their sexual orientation and gender identity? To hit, torture, or possibly kill them? Because that is what you are supporting, if you vote against this resolution.”

Early in the session, Saudi Ambassador Faisal Trad brought a “no-action motion” to quash any debate on the resolution, but his move was defeated.

Trad argued against what he called “the imposition of certain ideas” and said the new post would open up a “Pandora’s box” while ignoring cultural and religious specificities.

“We will not barter man-made legislation against divine laws,” Trad said, invoking sharia (Islamic law).

Nigeria called the resolution “divisive” and said that the sponsors also wanted to promote same-sex adoptions.

Pakistan - speaking on behalf of the Organisation of Islamic Cooperation (OIC) of 57 states - decried the “promoting of certain notions, concepts and lifestyles on which there is no consensus”.

In 2011, the U.N. rights body declared there should be no discrimination or violence against people based on their sexual orientation. At the time, Western countries called the vote historic but Islamic states firmly rejected it.