A far-right group Pro-Cologne had called the rally to oppose a decision by local authorities in Cologne to allow the construction of a mosque with a high dome and minarets.
It invited nationalist groups from around Europe to join the "Stop Islam" rally to fight what it called the "Islamisation and immigration invasion" of Germany and Europe.
But only 50 supporters of the anti-immigrant group Pro Cologne managed to reach a city square for the scheduled rally against plans to construct a grand mosque in the German city.
An anti-right sit-down by 5,000 mostly peaceful demonstrators had blocked every entrance to the square. At the same time, police were fighting pitched battles with extreme leftists who tried to occupy the square.
The "Stop Islam" rally did start briefly, with more reporters than rightists attending but then city police declared the event illegal on public-safety grounds.
"The rally has been cancelled," a police spokesman said. The announcement sparked cheers from many protesters.
"The safety of our Cologne people has priority," said a police spokesman after ugly clashes between far-leftists and riot police.
The leftists, who were bent on occupying a city square set aside for the rightists to use, assaulted police and tried to snatch their pistols. Riot police advanced against them, swinging batons.
At Heumarkt, the square set aside for the rightists, the far left attacked roadblocks at several places and scuffled with riot police, but were repulsed.
"We had to crack down hard to avoid something worse happening," a police spokesman said. Reporters saw two men being detained. A police officer was hurt in the face when a firecracker was thrown at him.
Expecting trouble, riot police kept water-cannon trucks that can knock a man to the ground at 30 meters at the ready.
A police spokesman said the leftists were no longer attacking in small groups but in large formations.
Television images showed that only about 50 rightists managed to pass the blockade and enter the city square, where "no mosque" banners were hanging.
Rally inflames passions in Muslim nations
About a kilometer away, several thousand peaceful demonstrators listened as Cologne's mayor Fritz Schramma stood up outside the city's great cathedral to denounce the far-right rally.
City and national German authorities detest the rightists, but say they must allow them the right of free speech.
Schramma described those who bent on holding the far-right rally as "racists in bourgeois dress" and "the moldy clique of Euro-Fascism," adding: "I say, there is the door. Go home!"
The planned anti-mosque demonstration had not only inflamed passions in Germany, but in Muslim nations. Iran demanded that Germany prohibit it. But German police and lawyers said it could not be banned purely because of the opinions to be expressed.
The mayhem also hit rail links. A signal box on the edge of town was set on fire in the early morning by persons unknown, forcing the closure of a main line for several hours and the diversion of 12 national express trains.
The line was re-opened after hasty repairs.
Rightist group formed to oppose "Islamification" of Cologne
Pro Cologne, a local group which won 5 per cent of votes at the last city-council elections, said Saturday it was outraged by the decision to cancel the congress.
Its secretary, Markus Wiener, said, "It's typical of the Cologne police leadership that they can't enforce freedom of assembly and that they cave in to street terrorism."
Wiener said his group had had 1,000 supporters trying to attend the rally.
A city councillor for the group said he would challenge the ban in court.
"We'll repeat the event later," Manfred Rouhs told WDR television.
Police had banned Pro Cologne from marching Friday evening to multi-ethnic neighborhoods, saying that riot police would not be able to keep order.
There was no sign in the city of prominent far-rightists from abroad who had been invited, such as Jean-Marie Le Pen, the founder of the National Front in France.
"A victory for democracy"
Cologne Mayor Schramma, who has personally backed the mosque project, welcomed the rally ban.
"It's a victory for the city of Cologne and a victory by the democratic forces in this city," he told Dpa news agency.
Armin Laschet, minister for minorities in North Rhine-Westphalia state, went further, telling the Tagesspiegel newspaper it was the first time an entire German city "stood up to protect its Muslims."
The German Interior Ministry spoke out Friday against the rally, saying the planned gathering of "populists and extremists harms the co-existence that the city and Muslim citizens have striven for."
The mosque, close to a soaring telecommunications tower in a district on the edge of town, is to have a dome 37 meters high and two minarets stretching up 55 meters. It will serve Turkish-speakers whose current mosque is too small for the congregations.
Ditib, an Ankara-funded organization that builds mosques all over Germany, says the current mosque at its national headquarters in Cologne is too small for congregations.