BERLIN — A man wielding a large knife fatally stabbed one person and wounded four others on Friday during an attack in and around a supermarket in Hamburg, Germany, the police said. The assailant was arrested as he tried to flee.

The police described the attacker only as a young man, who they said was acting alone. They did not provide any details about the man and declined to offer any information to suggest a motive for the attack, but said they had not ruled out the possibility that it was an act of terrorism.

Germany has been the target of several attacks by Islamist extremists over the last year, and officials have expressed concern about people who have returned to the country after fighting for the Islamic State in Syria or Iraq. Officials with the country’s Federal Criminal Office believe that there could be as many as 274 returnees, not all of whom they have enough evidence to detain.

There are also fears that terrorists may have entered the country during the wave of mass immigration in 2015.

A woman identified as a cashier at the Hamburg supermarket in the district of Barmbeck told the private 24-hour news channel n-tv that the man had shouted “Allahu akbar,” which means God is great in Arabic, after he attacked people inside the store.

The man wounded other passers-by on the street after he ran out of the supermarket, before he was detained by the police. The German news media showed images of him in the back seat of a police car, his face covered by a cloth stained with blood, which the police said came from injuries he sustained.

Last summer, a string of assaults, including one by an unaccompanied minor refugee who attacked passengers on a train with an ax, and another refugee who detonated a backpack of explosives near a bar in southern Germany, rattled the country. Both attackers had pledged allegiance to the Islamic State, as did Anis Amri, the man who drove a truck into a Christmas market on Dec. 19, killing 12 people in the country’s worst terrorist attack in decades.