It was the Danes who finally wore the Majid family down.

The family had fled war-torn Syria, taken a boat from Turkey to Greece, crawled under a barbed wire fence in Hungary, and slept in fields and on concrete sidewalks. The relatives thought that after so much hardship the trip through Denmark to Sweden would be easy.

After all, they had no intention of staying in Denmark. Why would the Danes care if they were just passing through?

They were wrong.

As the family crossed the Danish border from Flensburg, Germany, on Monday, the police stopped the train and took all the refugees and migrants off. Ahmad Majid, his brother Farid, their wives, children and other relatives traveling with them were taken to a makeshift detention center at a decommissioned school in Padborg, a truck-stop town of little cottages near the border.

At the school, the Danes treated them with an iron fist in a velvet glove. The migrants were given thin foam mattresses, blankets, hot food and balls for the children to play with. But armed police guarded every door, sending a message that the migrants were prisoners there, not free to come and go as in the German shelter where the family had spent a night.

The police told them they had a choice: Stay in Denmark and apply for asylum, or return to Germany. But they would not be permitted to cross Denmark to go to Sweden.

The Majids were still undecided about their plans when they got a taste of Danish justice. They were told that before they could go anywhere, all the migrants detained at the former school had to go to a police station for processing. The family was divided into two groups, the two brothers with their wives and children in one, the three young male relatives traveling with them in another. It was the first time they had been divided despite crossing many borders since their flight from Syria.

The group with the two brothers was taken by a police van to a station far away, they said later. Ahmad Majid estimated that he and his family had traveled more than 200 kilometers, based on the amount of time they were on the road. They had no idea where they were. Once at the station, they were put in separate cells.

When Mr. Majid asked for food for the children, offering 100 euros to pay for it, a police officer gave him sugar cubes, he said, fishing the still-wrapped cubes out of his jacket pocket.

The three young men were taken to another police station, also far from the Padborg school, based on the travel time. There they were ordered to strip naked and twirl around in front of police officers, they said. Then they were locked up in two cells.

Mr. Majid and his brother were shaking with anger by the time the police interviewed them about whether they wanted to seek asylum in Denmark or return to Germany. At that point, they said, the invitation to seek asylum in Denmark seemed insincere, like a bad joke.

“Even if Denmark was made of gold, I would not want to stay here, ever,” Farid Majid said he had told the Danish police.

Ahmad Majid told the police, “My son now knows the meaning of the word ‘prison,’ because of you.”

The police warned that if the Majids came back to Denmark, they would be imprisoned.

The young men also rejected the Danish asylum offer. The police took all of them back to the German border. From there, they made their way to the Flensburg train station.

We caught up with the Majids at the Flensburg station on Wednesday night, as they waited for a train to Hamburg.

They had missed the big events at the Padborg school. That morning, 300 refugees had stormed out of the school, into the woods and streets. About half had disappeared into the countryside, and the others had found their way to the highway to Sweden. They blocked traffic as they walked north.

As the Majids were boarding the train for Hamburg, the police back in Denmark were trying to figure out how to deal with the public relations embarrassment of having migrants block the highway and demand to go to Sweden.

The Majids got to Hamburg just before midnight and headed for McDonald’s, about the only restaurant open in the train station, to feed the children. On the way, Mr. Majid approached two German police officers and asked where he could turn himself in to ask for asylum in Germany. The officers pointed to a police station 200 meters away.

But before they could turn themselves in, Philip Holler, a young law student, approached and asked whether they were refugees. Yes, they said. He said he could show them the way to a shelter for the night. They accepted his offer. Mr. Holler waited patiently until the children had finished their chicken nuggets. Then he led the exhausted group into the subway and through the darkened Hamburg streets to a hangar-like exposition center that was being used as a shelter for migrants.

The shelter's beds were full. The Majids waited about an hour, while the guards looked for another shelter that could take them. It was cold outside. The guards distributed juice, water and blankets, and the Majid adults wrapped the blankets around the children like cocoons and laid them on the sidewalk to sleep. Finally, at about 2:30 a.m., the shelter took them in. The gates closed behind them.

On Thursday morning, the Danish police announced that they would no longer block migrants from traveling through Denmark to Sweden and points beyond. As in Budapest a week earlier, when Hungary relented and opened its border to Austria, the migrants had won, at least for now. The police said 3,200 refugees and migrants had entered Denmark since Sunday, overwhelming the ability of the police to deal with them.

The Danish about-face came too late for the Majids, who were just relieved to put that country behind them. Yes, their dream had been to go to Sweden, join relatives, and maybe open a bakery or a grocery. But now Germany seemed like the land of opportunity. It had welcomed them and others like them, and its generosity, they had learned, was a rare gift. They would stay here, Ahmad Majid said, and they would thrive.

Their journey did not turn out the way they had planned. But he believed it had turned out for the best. He cited the Quran: “Do not hate what befalls you, for it may be good for you.”