Police shot dead a 22-year-old Danish-born gunman on Sunday after he killed two people at a Copenhagen synagogue and an event promoting free speech in actions possibly inspired by an attack on the French satirical weekly Charlie Hebdo, authorities said.
Spy chief Jens Madsen said the gunman was known to intelligence services prior to the shooting and had probably acted alone. Police said he had a record of violence, gang-related activities and weapons possession.
Two civilians - a synagogue guard and a film-maker - were killed and five police were wounded in the two separate attacks in the Danish capital on Saturday and Sunday.
Witnesses said the gunman had fired up to 40 shots at a cafe hosting a free speech event with Swedish artist Lars Vilks, who has received death threats for depicting the head of the Prophet Mohammad on a dog.
The gunman then moved on to a nearby synagogue where the guard, protecting a young girl's confirmation, was gunned down.
On Sunday, thousands of Danes left a sea of flowers by the city's ornate 180-year-old synagogue.
"We are a small nation and such things don't happen here," 28-year-old student Frederikke Baastrup said, reflecting a widespread sense of shock in a country that prides itself on its reputation for safety and social tolerance.
Police cordoned off several sections of a predominantly immigrant neighborhood and took away several people for questioning, witnesses said.
Danish media widely reported the gunman to be Omar Abdel Hamid El-Hussein. Reuters could not confirm his identity and police declined to comment.
On alert since Paris attacks
Danish media said El-Hussein had been jailed for stabbing a 19-year-old man in the leg on a Copenhagen train in 2013, and was freed a few weeks ago.
Danish authorities have been on alert since Islamist gunmen killed 17 people in three days of violence in Paris in January that began with an attack on weekly Charlie Hebdo, long known for its acerbic cartoons on Islam, other religions and politicians.
"Denmark and France are the same nations, feeling the same sadness but also the same will to resist, fight and defeat terrorism," French President Francois Hollande said.
"They hit the same targets, they hit what we are, what we represent, the values of freedom, the rule of law, that all citizens, whatever their religion, should be able to enjoy."
Madsen said the attacks appeared to have been inspired by the Paris attacks.
But police said they did not believe the suspect had received training in jihadist camps in the Middle East.
The man had two handguns on him when he was killed and the police search later found an automatic weapon that may have been used in Saturday's attacks.
The gunman's primary target was likely to have been the free speech event with Vilks.
Dozens of bullets were fired in quick succession, probably from an automatic weapon, according to a recording of the event obtained by Danish TV2.
Prime Minister Helle Thorning-Schmidt said the attacks were terrorism but said this was not the start of a war between the West and Islam.
"When you mercilessly fire deadly bullets at innocent people taking part in a debate, when you attack the Jewish community, you attack our democracy," Thorning-Schmidt said outside the synagogue. "We will do everything possible to protect our Jewish community."
Denmark became a target of violent Islamists 10 years ago after the publication of cartoons lampooning the Prophet Mohammad, images that led to sometimes fatal protests in the Muslim world. Many Muslims consider any representation of the Prophet blasphemous.
Vilks stirred controversy himself in 2007 with drawings depicting Mohammad's head on a dog, triggering death threats.
Israeli Prime Minister Benjamin Netanyahu said such attacks were likely to continue, and that Israel would welcome European Jews who chose to move to there.
Witnesses said French ambassador Francois Zimeray had just finished introducing the cafe event, entitled "Art, Blasphemy and Freedom of Expression", when the assailant opened fire.
The venue was heavily guarded by police, who fired back, but the attacker nevertheless escaped.
Vilks sheltered on the floor of a cold room at the back of the cafe with one of the event's organizers.
He has lived under Swedish police protection since 2010 and two years ago an American woman was jailed for 10 years in the United States for plotting to kill him.
Like other European governments, Scandinavian leaders have been increasingly concerned about the radicalization of young Muslims traveling to Syria and Iraq to fight alongside violent jihadist groups such as Islamic State.
Authorities have also been worried about possible lone gunmen like Anders Behring Breivik, the anti-immigrant Norwegian who killed 77 people in 2011, most of them at a youth camp run by Norway's ruling center-left Labour Party.