The appeals court in San Francisco overturned earlier rulings by immigration courts.
The three-judge panel said Jose Boer-Sedano, 45, would probably face further abuse in Mexico and have difficulty getting Aids treatment.
"It really does mean that he'll be safe now," said Mr Boer-Sedano's lawyer.
Angela Bean said her client was overcome with emotion at the court's decision.
Mr Boer-Sedano testified that in his home country he had been forced to perform sex acts by a police officer who threatened to kill him or expose his homosexuality.
He said he had been shunned by his family.
"Despite his attempts to conceal his sexuality, others could perceive it and Boer-Sedano was ostracised by his family, friends, and co-workers on that basis," the judges wrote in their judgement.--