It is lawful for transgender women to be housed in female jails in England and Wales, the High Court has ruled.
A female prisoner, known as FDJ, had challenged the Ministry of Justice over aspects of the policy.
She claimed she had been sexually assaulted by a trans prisoner but the MoJ did not say whether it accepted this alleged incident had taken place.
The judge ruled barring all trans women from female prisons would ignore their right to live as their chosen gender.
Women's prisons can house inmates who were born male but identify as female, regardless of whether they have gone through any physical transformation or have obtained a gender recognition certificate.
The MoJ argued the policy pursued a legitimate aim, including "facilitating the rights of transgender people to live in and as their acquired gender (and) protecting transgender people's mental and physical health".
The claimant in the case, FDJ, had said she was sexually assaulted in prison in 2017 by a trans woman with a gender recognition certificate (GRC), who had convictions for serious sexual offences.
The claimant's lawyers argued that placing transgender women in the female prisons exposed others to higher risk, citing a claim that transgender inmates were five times more likely than non-transgender prisoners to commit a sexual assault on a non-transgender prisoner.
In a judgement handed down via email, Lord Justice Holroyde accepted the statistical evidence showed the proportion of trans prisoners convicted of sexual offences was "substantially higher" than for non-transgender men and women prisoners
But he said the lawyers' claims about the risk of sexual assault were a "misuse of the statistics, which... are so low in number, and so lacking in detail, that they are an unsafe basis for general conclusions".
Between 2016 and 2019, 97 sexual assaults were recorded in women's prisons, the judgement said. Of these, it appears that seven were committed by transgender prisoners without a GRC. It is not known whether any were committed by transgender women with a GRC.
As of March 2019, there were 34 transgender women without GRCs allocated to a woman's prison. The number of transgender prisoners with a certificate is thought to be in single-figures across the prison population as a whole.
The judge said he "fully understood" the concerns of FDJ, and that women prisoners "may suffer fear and acute anxiety" if housed with a transgender woman who has male genitalia.
But he added that the rights of transgender women prisoners must also be considered.
"The unconditional introduction of a transgender woman into the general population of a women's prison carries a statistically greater risk of sexual assault upon non-transgender prisoners than would be the case if a non-transgender woman were introduced.
"However, the policies require a careful, case by case assessment of the risks and of the ways in which the risks should be managed," he said.
"Properly applied, that assessment has the result that non-transgender prisoners only have contact with transgender prisoners when it is safe for them to do so."
The claimant, FDJ, said she was "disappointed" with the judgement.
"By bringing this challenge, I did not seek to prevent trans women in prison from living in dignity, or to exclude all trans women from women's prisons. However, I feel that trans women who have a history of violence and sexual offending against women should not be in a situation where they can put our safety at risk."
A Ministry of Justice spokesperson said: "We are pleased the court agrees that the way we manage transgender offenders strikes the right balance between protecting their legal rights and ensuring the safety of all prisoners."
This is one of the first cases to enter the disputed terrain where, in the terms used by the court, the rights of women and the rights of transgender women compete.
The judge recognised that many women prisoners may suffer fear and acute anxiety if sharing prison accommodation with a transgender inmate.
But transgender women in male prisons may also suffer fear and acute anxiety. Prisons will, by their nature, include violent and dangerous individuals who present a risk to other inmates.
The question was whether the Ministry of Justice policy of placing transgender women among the general female prison population could be applied in a way that was not unlawfully discriminatory.
In the end, the judge accepted there was "a margin of discretion" in balancing the competing rights and managing the risks.
However, while the policy itself is ruled to be lawful, the judgement does make it clear that individual decisions are still open to challenge.