An Egyptian court has acquitted 26 men arrested in a televised raid last month by police looking for gays at a Cairo public bathhouse.

The ruling set off deafening cheers and jubilation inside the courtroom as some of the defendants uncovered their faces and cried with relief.

The trial, which had caused uproar among activists and rights groups, captured public attention after a pro-government TV network aired scenes of half-naked men being pulled from the bathhouse by police.

Same-sex relations are not explicitly prohibited in Egyptian law but homosexuality is a social taboo in the conservative, Muslim-majority country. Same-sex marriage is unheard of. Only in recent years have films and fiction included gay characters.

The men in the bathhouse raid faced various charges, including debauchery and performing indecent public acts. The verdict came after only three hearings, during which families quarrelled with journalists who tried to photograph their relatives in the dock.

The courtroom erupted into a frenzy after the word "acquittal" was heard from the judge and women ululated. Scott Long, an American researcher who had followed the case, said he was both "shocked and delighted".

"I hope this is a sign that these raids will come to an end," Mr Long told the Associated Press amid the cheering. "Finally there was a judge who listened to the evidence."

Rights activists say 2014 was the worst year in a decade for Egypt's gay community, with at least 150 men arrested or put on trial.

"They destroyed our lives. God rescued us," said one of the defendants.

The trial opened only two weeks after the December 7 raid on the bathhouse, or hammam, after quick referral by the general prosecutor.

There are no laws in Egypt criminalising homosexuality but a decades' old law criminalising prostitution is often used in penalising the gay community. Five of the defendants in Monday's trial - the owner of the bathhouse and four staff members - were tried for facilitating debauchery in exchange for money.

In the official charges, the prosecutor said the investigation revealed the owner and the staff ran the bathhouse as a place for "parties of debauchery, orgies among male homosexuals in exchange for money". The rest of the defendants were charged with practising debauchery and "indecent public acts".

The crackdown on the gay community in Egypt, and also recently on atheists, goes hand in hand with a wider campaign against all forms of dissent and diversity in a country gripped by rising nationalism and a militant insurgency.