Two gay men who said they faced persecution in their home countries have the right to asylum in the UK, the Supreme Court has ruled.
The panel of judges said it had agreed "unanimously" to allow the appeals from the men, from Cameroon and Iran.
They had earlier been refused asylum on the grounds they could hide their sexuality by behaving discreetly.
Home Secretary Theresa May said the judgement vindicated the coalition government's stance.
Under the previous government the Home Office had contested the case, saying it had taken sexuality into account when making its decisions.
The five Supreme Court justices were asked to decide whether a gay applicant could be refused asylum on the grounds that he could avoid ill treatment by concealing his sexuality.
Previous attempts by the men to stay in the UK had been rejected by judges at the Court of Appeal who ruled that if the men could conceal their sexuality, their situation could have been regarded as "reasonably tolerable".
But the applicants said this tolerability test was contrary to the Refugee Convention, to which the UK is a party.
The Supreme Court agreed and ruled that the men's cases could be reconsidered.
Lord Hope, who read out the judgement, said: "To compel a homosexual person to pretend that his sexuality does not exist or suppress the behaviour by which to manifest itself is to deny his fundamental right to be who he is.
"Homosexuals are as much entitled to freedom of association with others who are of the same sexual orientation as people who are straight."
The court said it would be passing detailed guidance to the lower courts about how to treat such cases in the future.
The applicant from Cameroon, who is only identified as HT, had been told he should relocate elsewhere in his country and be "more discreet" in future.
He had been attacked by an angry mob at home after being seen kissing his partner. He has been fighting removal from the UK for the past four years.
"Some people stopped me and said 'we know you are a gay man'," HT earlier told the BBC.
"I cannot go back and hide who I am or lie about my sexuality."
The other application was from a 31-year-old Iranian gay man, who was attacked and expelled from school when his homosexuality was discovered.
Like HT, he had been told he could be "reasonably expected to tolerate" conditions back home that would require him to be discreet and avoid persecution.
Punishment for homosexual acts ranges from public flogging to execution in Iran, and in Cameroon jail sentences for homosexuality range from six months to five years.
Mrs May said she welcomed the ruling, adding that it was unacceptable to send people home and expect them to hide their sexuality.
She said: "We have already promised to stop the removal of asylum seekers who have had to leave particular countries because their sexual orientation or gender identification puts them at proven risk of imprisonment, torture or execution.
"From today, asylum decisions will be considered under the new rules and the judgement gives an immediate legal basis for us to reframe our guidance for assessing claims based on sexuality, taking into account relevant country guidance and the merits of each individual case.
'We will of course take any decisions on a case-by-case basis," she said.
Ben Summerskill, the chief executive of gay lobby group Stonewall said it was delighted and offered to help the government deal with such cases.
Its recent No Going Back report had suggested that between 2005 and 2009, the Home Office had initially refused 98% of all gay or lesbian asylum claims.
Mr Summerskill said: "Demanding that lesbian or gay people return home to conceal their sexuality bears no resemblance to the reality of gay life in many countries."
Donna Covey, chief executive of the Refugee Council agreed and said: "It is about time refugees fleeing their countries because of persecution over their sexuality are acknowledged as being legitimately in need of safety here, in line with those fleeing other human rights abuses."
The charity Refugee Action called for UK Border Agency staff to receive further training about issues that could affect gay people in their home countries.
Its chief executive Jill Roberts said: "It is crucial that the right decision is made first time so that people are not returned to danger."