After shutting down the main corridor for refugees to Central Europe and fortifying its border with 109 miles of razor-wire fencing, Hungary on Tuesday began mass arrests of migrants who tried to sneak across its frontier.

More than 150 were detained as the country drew its line in the sand. Its get-tough approach to the European refugee crisis created an immediate bottleneck and howls of protest.

Within hours, a tent city sprouted on the Serbian side of the border, creating another potential flash point of misery and anger, as refugees scrambled for new routes to their preferred destinations in Germany and Scandinavia.

Hungary’s move was condemned by neighboring countries, humanitarian organizations and migrants, who staged spontaneous demonstrations along the newly erected fence.

Yet Hungarian Prime Minister Viktor Orban and his government made good on their promise to take a hard line, insisting that refugees who had escaped Syria’s civil war, for example, were safe once they had reached Turkey or Jordan and did not need to march through Hungary on their way to wealthy, welcoming nations such as Germany.

From a Hungarian outpost at the border fence, a voice shouted in Arabic through a loudspeaker: “We are not opening the border!”

Hungary dispatched the first of 800 new border guards to the fence, as authorities declared a state of emergency in two counties bordering Serbia, a move that would allow the parliament to deploy troops to the region.

Gyorgy Bakondi, a Hungarian security adviser, said authorities detained at least 155 people trying to cross into the country, some of whom were attempting to damage portions of the fence.

New rules in Hungary also put smuggling networks on notice, increasing prison terms for convicted human traffickers.

The refugees kept coming — and dying along the way.

At least 22 migrants were reported drowned in the Aegean Sea after their 65-foot boat capsized as they were trying to reach the Greek island of Kos. Among the dead were four children.

The Turkish coast guard said 249 people were rescued in that incident, just two days after 34 refugees drowned in a similar sinking.

Migrants chanted “Save us from drowning” at Istanbul’s main bus station as they demanded transport to the border crossing near Greece and Bulgaria, about 100 miles to the northwest, possibly opening up new routes through the Balkans, the Reuters news agency reported.

Diplomatic fallout

Hundreds of thousands of men, women and children — many from war-racked Syria and Iraq — have been racing to reach Western Europe before their path is blocked by tighter border controls and a dramatic rollback of the European Union’s hallmark policy of open movement across frontiers.

The crackdown in Hungary also brought diplomatic fallout.

Serbia’s foreign minister, Ivica Dacic, denounced the Hungarian moves as “unacceptable” and insisted on talks with the European Union.

“We want to be part of the solution, not collateral damage,” said Dacic, whose nation is not an E.U. member.

Austrian Chancellor Werner Faymann also criticized Hungary, saying at a news conference, “You can’t just stick your head in the sand.”

Speaking to reporters, German Chancellor Angela Merkel defended her country’s open-door policy for refugees, saying Germany had shown its “friendly face” to the world.

Merkel also said Germany acted properly when it began to enforce immigration controls at its border with Austria, which led to huge traffic snarls and canceled trains in Austria.

At Vienna’s central railway station, where many were stranded, crowds awaited word on whether stalled buses and trains would again move toward Germany, whose generous social programs and giant economy draw many asylum seekers.

Austria’s Interior Ministry said the country was running out of shelter for refugees, with almost every one of 20,000 beds filled.

“We’re hoping for a decision by the Germans that they will accept us again,” said Mohammad, 24, from Fallujah, Iraq. He declined to give his last name for fear of reprisals against his family back home. He had reached the heart of Europe without showing anyone any documents, he said.

Transit zones

In Washington, President Obama said the United States and its allies “may have to deal with the source of the problem, which is the ongoing crisis in Syria.”

The United States, he said, “feels it is important for us to also take our share of Syrian refugees as part of this overall humanitarian effort.” He gave no details.

A Hungarian government spokesman, Zoltan Kovacs, told reporters that Hungary would create new transit zones to hold asylum seekers while their applications are processed. Human rights activists predicted that most asylum claims would be quickly rejected by Hungary.

At the Hungarian border town of Roszke, several hundred refugees staged a peaceful protest, sitting on the highway and holding signs saying they would refuse food and water until they were allowed to continue their journey.

“We just want to pass through their country,” said Amir Kirit, 25, a software engineer from Damascus, Syria. “There is no going back now. We took the most dangerous trip in the world to get here because in Syria there is just death. So we will die here if we have to. Women, children. We need the international community to help.”

Across the border in Serbia, the needs were even starker. Hundreds of migrants spent the night in tents or curled up on yoga mats in the open.

Volunteers braced for another potential humanitarian crisis, with shelter and food in short supply.

A record 156,000 migrants entered the European Union in August, the bloc’s border agency, Frontex, said Tuesday. That brought the total for the year to more than 500,000, with many arriving by sea in Greece and Italy.

Last year, 280,000 migrants crossed into the E.U., the agency said.

Booth reported from Vienna and Budapest. Souad Mekhennet in Vienna, Gergo Saling in Horgos and Brian Murphy in Washington contributed to this report.

Read more:

Germany’s enthusiasm for refugees might not last. These maps explain why.

Refugees get a chance to wash and rest their weary soles in Budapest

5 reasons Germany reintroduced border controls

Read The Post’s coverage on the global surge in migration