Gay and lesbian asylum seekers have won the right not to be deported from the UK if they would be persecuted in their home countries.

The supreme court unanimously allowed appeals from two men, from Cameroon and Iran, whose claims had earlier been turned down because officials said they could hide their sexuality by behaving discreetly.

The government accepted the ruling and said that policy on gay and lesbian asylum seekers would be changed with immediate effect.

Lord Hope, heading the panel of five judges, said that to force a gay man to pretend his sexuality did not exist or should be suppressed was a breach of his fundamental rights. The court also laid down a framework on how asylum claims by gay and lesbian people should be determined.

The ruling was welcomed by the home secretary, Theresa May, and equality campaigners. May 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.

"I do not believe it is acceptable to send people home and expect them to hide their sexuality to avoid persecution. From today asylum decisions will be considered under the new rules and the judgment 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."

John Wadham, the legal director of the Equality and Human Rights Commission, which intervened in the case before the court, said: "A gay person should be allowed to live openly if they choose. Concealing their sexual identity to avoid persecution is not something they should be forced to tolerate."

Jill Roberts, the chief executive of the charity Refugee Action, said the present system "was effectively asking gay people to deny their own identity and live with the daily threat of discovery".

Ben Summerskill, the head of the campaign group Stonewall, said demanding that lesbian or gay people conceal their sexuality bore "no resemblance to the reality of gay life in many countries".

"We're delighted the government is responding to what we asked of all the political parties in the run-up to the election. Our report No Going Back shows that UK Border Agency staff urgently need better guidance and support to deal with cases involving gay asylum seekers."

One of the men, known as T, was challenging an appeal court decision that he could return to his native Cameroon even though he had been attacked by a mob after he was seen kissing a male partner.

The other, known as J, had been told he could be expected to tolerate conditions arising from his homosexual relationship in Iran, his home country. He was advised to behave discreetly to avoid reprisals.

Punishment for homosexual acts ranges from public flogging to execution in Iran. In Cameroon jail sentences for homosexuality range from six months to five years.

The Convention on the Status of Refugees provides that members of social groups are entitled to asylum in states that are parties to the convention if they can establish a well-founded danger of persecution if returned to their home country.

The court of appeal had found both men could hide their sexual orientation to avoid persecution. Neither had a "well-founded fear of persecution" that entitled them to protection.

However, Lord Hope, deputy president of the supreme court, said that persecution of homosexual people was not regarded as a problem when the convention was drafted because for many years it was the practice in some countries to deny its existence.

Hope said in his ruling: "This was manifest nonsense but at least it avoided the evil of persecution. More recently, fanned by misguided but vigorous religious doctrine, the situation has changed dramatically. The ultra-conservative interpretation of Islamic law that prevails in Iran is one example. The rampant homophobic teaching that rightwing evangelical Christian churches indulge in throughout much of sub-Saharan Africa is another."

A "huge gulf" had opened up in attitudes towards gay people, he said. "It is one of the most demanding social issues of our time. Our own government has pledged to do what it can to resolve the problem, but it seems likely to grow and to remain with us for many years." More gay and lesbian people were likely to have to seek protection in this country if it was denied in their home countries, he said.

Another member of the court, Lord Rodger, said normal behaviour of gay people must be protected just as it was for heterosexual people. "What is protected is the applicant's right to live freely and openly as a gay man. To illustrate the point with trivial stereotypical examples from British society: just as male heterosexuals are free to enjoy themselves playing rugby, drinking beer and talking about girls with their mates, so male homosexuals are to be free to enjoy themselves going to Kylie concerts, drinking exotically coloured cocktails and talking about boys with their straight female mates."

The justices said immigration tribunals should in future decide on the evidence whether an applicant was gay and whether he would face persecution if he lived openly in his own country. If this were the case, then he would have a well-founded fear of persecution, even if he could avoid the risk by living discreetly.

If the applicant chose to live discreetly because of social pressure – not wanting to distress his parents or embarrass his friends – then the application should be rejected. If the tribunal found the applicant would have to live discreetly to avoid persecution, then his application should be allowed. Tribunals should follow the same approach for lesbians.