By Maja Zuvela and Krisztina Than

SID, Serbia/ROSZKE, Hungary (Reuters) - Hungary on Wednesday detained 29 people including a "terrorist" as migrants demanding to be let through the country's newly shut EU frontier clashed with riot police firing water cannon and tear gas while refugees searched for new ways to enter the bloc.

Hungary's decision this week to shut the EU's external border with Serbia was the most forceful attempt yet by a European country to reduce the flood of refugees and economic migrants overwhelming the bloc.

As thousands of migrants scattered across the Balkan peninsula tried to reach the EU, Hungary's prime minister said his country planned to put up a fence along parts of its border with Croatia and on the frontier with Romania to stem the flow.

Helmeted riot police backed by armored vehicles took up positions at the barricaded border crossing with Serbia, where male migrant youths pelted them with stones, demanding entry.

Hungary said it detained a "terrorist" among 29 migrants held during the clashes. At least 20 policemen and two children were injured, a Hungarian security official said.

"Police also captured an identified terrorist," Gyorgy Bakondi, a security adviser to Hungarian Prime Minister Viktor Orban, told state television M1. A government spokesman said the man was “in the database of security services”.

"It is getting very ugly there," said Ahmad, 58, a shopkeeper from Baghdad who went to the official border crossing at Sid in Serbia but realized he may have a better chance of entering the EU via Serbia's border with Croatia.

"As soon as we heard about a route to Croatia we did not wait long. I want to go to Sweden to meet the rest of my family. I hope we will be treated better in Croatia," he told Reuters.

Serbia's prime minister accused Hungary of "brutal" and "non-European" behavior and urged the EU to respond.

"I call on the European Union to react, for its members to behave in line with European values," Aleksandar Vucic told Serbian state television during a visit to the United States.

Story continues

"If the EU does not react, we will find a way to protect our borders and European values as well," he said.

Serbia said it would send extra police to the border and try to distance migrants from the fence, as three Hungarian military Humvees mounted with guns stepped up security on the other side.

U.N. SHOCKED

U.N. Secretary-General Ban Ki-moon expressed shock and alarm at the treatment of refugees and migrants at Hungary's border with Serbia. The Council of Europe human rights body said it had asked Hungary to explain its new legislation on the crisis.

Antonio Guterres, the U.N. High Commissioner for Refugees, urged Hungarian authorities to ensure "unimpeded access" for people as they flee wars and persecution.

"UNHCR was especially shocked and saddened to witness Syrian refugees, including families with children who have already suffered so much, being prevented from entering the EU with water cannon and tear gas," it said in a statement.

Serbia's border with Hungary has until now been the main route for migrants, who come mostly by dinghy to Greece, then trek across the Balkan peninsula to reach the EU's frontier-free Schengen zone, most bound for Germany.

Migrants scattered through Balkan countries said they were seeking other routes, possibly through Croatia or Romania, which are in the EU though not in Schengen.

"If not Hungary, we will have to find another way. Most probably Croatia and from there we will see,” said 43-year-old Mehmed from Damascus, holding his three-year-old daughter after crossing into Macedonia from Greece.

Croatia said it would send experts to its Serbian border to identify minefields from the Balkan wars of the 1990s, the last time hundreds of thousands of displaced people marched across Europe.

The goal for most is Germany, which cut off trains from Austria on Wednesday to slow the flow of arrivals.

Tens of thousands of migrants have rushed to Austria in recent days to cross before Hungary shut the frontier. Austria said it would impose border controls on its frontier with Slovenia, along the likely new route from Croatia.

"FENCES, DOGS"

Hungary has thrown up a 3.5-metre (10-foot) fence along most of its border with Serbia and is working to extend it along the border with Romania, prompting Romanian Prime Minister Victor Ponta to evoke the continent's darkest era.

"Fences, dogs, cops and guns: this looks like Europe in the 1930s. And did we solve the refugee problem with this? No, we didn't," he said. "Erecting a fence only throws the problem into Serbia, into Croatia, into Romania."

At the Croatian border with Serbia, Reuters reporters saw hundreds of people, some of whom said they were Iraqi, trek through fields near the Sid border crossing.

The biggest flow of immigrants into Western Europe since World War Two has sown discord, fuelling the rise of far-right political parties and jeopardizing the 20-year-old achievement of Schengen's border-free travel.

Hungary says it is simply enforcing EU rules by sealing the Schengen zone's external border. It says Serbia is a safe country, so asylum seekers who reach the frontier there can be automatically turned back in a process that should take hours.

Hungary's Orban told Austrian newspaper Die Presse his formerly Communist country would erect fences at some places on the Croatian border as well as along the Romanian border.

The United Nations says Serbia lacks the capacity to receive refugees halted at the gates of Europe. Critics say Orban's anti-immigrant rhetoric has crossed the line into xenophobia.

The crisis has pitted countries that are comparatively open, led by Germany, against those, many in formerly Communist eastern Europe, who say the welcoming approach has made the problem worse by encouraging people to make dangerous voyages.

Hungary blames Germany for exacerbating the crisis by announcing in August it would suspend normal EU asylum rules and take in Syrian refugees regardless of where they enter the EU.

Thousands have since been trekking across the bloc, mainly through Hungary and Austria, to reach Germany, clogging railway stations and forcing trains to be canceled.

An emergency meeting of EU ministers this week failed to agree on a Berlin-backed plan to share out 160,000 refugees.

A German cabinet minister said on Tuesday the EU should consider financial penalties against countries that refuse to take their share, drawing angry responses from countries which oppose quotas, such as the Czech Republic and Slovakia.

Orban said in remarks published in German newspaper Die Welt on Wednesday that if the EU decides to impose mandatory quotas of asylum seekers, Hungary would have to comply.

Orban said Hungary was prepared to talk about quotas but on a voluntary basis and only when the influx of refugees stopped.

(Reporting by Reuters bureaux; Writing by Peter Graff and Matt Robinson; Editing by Alison Williams and James Dalgleish)