The men were arrested in the December 7 raid on a hamam in the Azbakeya district of the capital, amid fears of a widening police crackdown on gays in Egypt.
The raid was filmed by a female television journalist, who days later aired its footage on the "The Hidden," a weekly programme on pro-regime private satellite channel Al-Qahira Wel Nas.
The footage showed the near naked men, covering their faces and wearing only towels, dragged out of the hammam and loaded onto police trucks.
"Allahu Akbar (God is Greatest), Long live justice," chanted the defendants when the verdict was announced, an AFP correspondent reported from the court room.
"Long live justice and the police," cheered the jubilant families of the defendants, some of who clashed with reporters and photographers before the hearing began.
The defendants, including the bathhouse owner and four employees, were brought handcuffed to the court room and made to stand in a metal cage guarded by two rifle-wielding policemen.
"The ruling proved our innocence and cleared the name of the hammam. I swear we did nothing wrong," said Fathy Abdel Rahman, the owner.
"Finally, an Egyptian court issued a verdict in a case of this kind according to the law," Ahmed Hossam, a defence lawyer, told AFP.
Egyptian law does not expressly ban homosexuality, but gay men have previously been arrested and charged with debauchery instead.
In the past, homosexuals in Egypt have been jailed on charges ranging from "scorning religion" to "sexual practices contrary to Islam".
'DAMAGE ALREADY DONE'
Relatives of the defendants kissed policemen present in the court as they expressed joy on hearing the verdict.
Signup to our newsletter and follow us on Facebook and Twitter!
"Thanks to Allah, the truth is out ... my son was in the hammam with his friend to bath before his wedding. My son is a real man," said Hanan, a mother of one of the defendants.
Hassan Sherif, a gay man using a pseudonym, welcomed the verdict but warned that "the damage has already been done".
"The social stigma now attached to these men and their families will not go away. They have been humiliated in the media and in society," he said.
Relatives threatened to sue television presenter Mona al-Iraqi who filmed the raid.
"It's very obvious that Mona al-Iraqi fabricated the case, it's only right that the prosecution files a case against her for making a false report to the police," said lawyer Hossam.
"This will be the best rehabilitation for the defendants," he said.
"The case was fabricated and about to completely destroy 26 families. God revealed the truth," said Sayed, a brother of one of the defendants.
Iraqi has said on her Facebook page that airing the footage was not aimed at targeting homosexuality, but part of a "series uncovering male sex trafficking and the spread of AIDS in Egypt".
Defence lawyer Islam Khalifah said there was no evidence to convict the defendants.
"There was the police officer's story, and he is the only witness and the forensic report denied his version of the story," he said.
The forensic report submitted to the court states that none of the defendants showed signs of regular homosexual activities.
Advocacy groups such as New York-based Human Rights Watch have condemned the controversial anal tests carried out on suspected gays.
"This is amazing and unprecedented in Egypt," said Scott Long, a human rights activist who attended the trial, after the verdict was issued.
The verdict came weeks after a court in a separate case reduced the jail terms of eight men over an alleged gay wedding video that went viral on the Internet, slashing them to one year each from three years.
Their arrests in September were part of a series of highly publicised raids targeting suspected homosexuals in the deeply conservative Muslim country.