A Syrian man who was denied asylum in Germany a year ago died on Sunday when he set off a bomb outside a crowded music festival in the Bavarian city of Ansbach, injuring 12 other people, local officials said.

Bavaria's interior minister said the 27-year-old man detonated a backpack device after he was denied entry to the festival in Ansbach, a small town of 40,000 people southwest of Nuremberg that is also home to a US Army base.

The explosion came as Germany, and the southern state of Bavaria in particular, have been on edge. Just two days earlier, a man went on a deadly rampage at a Munich mall, killing nine people and leaving dozens wounded.

And an axe attack on a train near Wuerzburg last Monday wounded five. A 17-year-old Afghan asylum-seeker was shot and killed by police as he fled the scene.

Joachim Herrmann, the Bavarian interior minister, told reporters at a hastily convened news conference on Monday that it was unclear if the man who blew himself up in Ansbach had planned to commit suicide or "take others with him into death", according to news website Nordbayern.de.

Herrmann, whose remarks were confirmed by a ministry spokesman, said the Syrian man arrived in Germany two years ago and had tried to commit suicide twice before.

He added that the man's request for asylum was rejected a year ago.

The man was carrying a backpack filled with explosives and metal parts that would have been sufficient to kill more people, Herrmann said. Three of the 12 victims suffered serious injuries in the blast.

The minister said he could not exclude the possibility of an Islamist-inspired attack, but said that would have to be confirmed by an investigation.

Earlier, a spokesman for the prosecutor's office in Ansbach said the attacker's motive wasn't clear.

"If there is an Islamist link or not is purely speculation at this point," said the spokesman, Michael Schrotberger.

An official at the German interior ministry said the man was to be deported to Bulgaria, adding that Syrian nationals cannot be sent back to their country because of the situation there.

The ministry spokesman, Tobias Plate, said he could not say "at this moment why the deportation" of the 27-year-old failed asylum seeker didn't happen. It was not clear when he was supposed to be deported.

‘Loud bang’

More than 2,000 people were evacuated from the festival in Ansbach after the explosion, police said. A large area around the blast site remained blocked off hours later.

Ansbach resident Thomas Debinski said people panicked when they heard the explosion, especially after the events of the past week.

"Suddenly you heard a loud, a really loud bang, it was like an exploding sound, definitely an explosion," he said. "(People were) definitely panicking."

Debinski said it soon became clear that someone had set off a bomb in a rucksack.

It was the fourth violent incident in Germany in a week.

Earlier on Sunday, a 21-year-old Syrian refugee was arrested after killing a pregnant woman and wounding two people with a machete in the southwestern city of Reutlingen, near Stuttgart.

"After what just happened in Munich, and today in Reutlingen, what you hear about, it is very disturbing, when you know that such a thing can happen so close to you, in such a small town as Ansbach," Debinski said.

After the Munich attack, Herrmann urged the German government to allow the country's military to be deployed to support police during attacks. Germany's post-war constitution, because of the crimes of the Nazi era, only allows the military to be deployed domestically in cases of national emergency

Herrmann has called those regulations obsolete and said that Germans have a "right to safety".

Back in January, Bavaria's justice minister launched a state programme in Ansbach meant to teach refugees the basics of law in their new host country. The initiative came amid growing tensions and concerns in Germany about how it would integrate the estimated 1 million-plus migrants it registered crossing into the country last year.

Classes include lessons about freedom of opinion, the separation of religion and state and the equality of men and women.

"Germany is an attractive country because it respects the dignity of every human being," an educational film shown to newcomers said, "and it is supposed to stay that way."