Saturday's ruling suspends an interim injunction which had blocked the mayor from trying to ban a Marvel comic book on sale at the Rio book fair which shows a kiss between two male superheroes.
Ultra-conservative mayor Marcelo Crivella had ordered the book removed from sale because of its "sexual content for minors."
Crivella -- a former bishop in the giant Universal Church of the Kingdom of God -- was elected Rio's mayor in 2016, promising to bring law and order to a city beset by crime.
The drawing that sparked the mayor's ire showed the Marvel superhero characters Wiccan and Hulkling exchanging a kiss, fully dressed.
Crivella's move backfired as copies of "Young Avengers: the Children's Crusade" quickly sold out after he demanded it be withdrawn from the book fair, organizers told AFP.
Popular Brazilian YouTuber Felipe Neto, who has more than 34 million subscribers to his channel, also bought 14,000 books on LGBT themes and distributed them free at the fair in protest.
The image of the Marvel comic kiss was also on show Saturday at news kiosks across the country, printed on the cover of the Folha de S. Paulo, Brazil's largest national newspaper.
But on Saturday afternoon, the president of the Rio de Janeiro Court of Justice suspended the injunction, endorsing the actions of the mayor's office.
For Judge Claudio de Mello Tavares, it was not expected that a superhero comic would address issues of sexuality so it should come with a content warning.
"Because it is a superhero work -- attractive to a juvenile audience -- which addresses the issue of homosexuality it is necessary that parents be duly alerted" before deciding "whether or not the text fits their vision of how to educate their children," Tavares argued in his ruling.
The organizers of the book fair, which ends Sunday, said they would appeal to the Supreme Court to reverse the ruling.
Publishers and writers accuse the mayor of wanting to censor content.
The mayor's actions, and the judicial decision, may violate freedom of expression and discriminate based on sexual orientation, said constitutional law specialist Michael Mohallem, of the Getulio Vargas Foundation.
"Since the decision seems to be specifically aimed at prohibiting the circulation of magazines that show gay kisses (rather than any other), my interpretation is that it is motivated by discrimination, both by the mayor and the judge," Mohallem said.
Brazil's Supreme Court in June voted to criminalize homophobia, classifying it as a crime similar to racism.
It was an important step for sexual minorities in one of the most dangerous countries for LGBT people in the world but was criticized by far-right President Jair Bolsonaro, who has a history of homophobic remarks.