The ruling came after spectators wearing T-shirts criticising Brazil’s interim president, Michel Temer, were expelled from Olympic venues.
The judge said that removing people from the venues for displaying political messages was an infringement of their freedom of expression.
Organisers of the Rio Games are expected to appeal against the ruling.
Judge Joao Augusto Carneiro Araujo made the ruling after a request by the Ministerio Publico do Brasil, an independent body defending the public interest.
They argued that the International Olympic Committee, the Brazilian government and the Rio organising committee had overstepped their authority when they banned peaceful political protests from Olympic venues.
The International Olympic Committee said the rules were standard and part of their charter, according to which “no kind of demonstration or political, religious or racial propaganda is permitted in any Olympic sites, venues or other areas”.
But the Ministerio argued that spectators’ freedom of expression had been violated when they were told by members of the security forces to put away signs and T-shirts with political messages or face expulsion from the sporting venues.
The judge said only racists and xenophobic messages could be banned.
He said any incident of a spectator’s right to freedom of expression being curtailed would incur a fine of 10,000 reais (£2,445; $3,175).
Critics of Mr Temer have used the Games to express their opposition to the interim president, who took power in May when Dilma Rousseff was suspended as president pending an impeachment trial.
Ms Rousseff’s impeachment trial is expected to be held later this month or at the beginning of September.
Brazilians are divided into those who think she was the victim of an attempted coup and those who believe her Workers’ Party is deeply corrupt and that it was high time for the party to be removed from power.