Police shot dead a 22-year-old Danish-born gunman after he killed two people at a Copenhagen synagogue and an event promoting free speech.
His actions appeared to be inspired by the attack on the French satirical weekly Charlie Hebdo, authorities said.
Danish 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 37-year-old synagogue guard and a 55-year-old film-maker – were killed and five police were wounded in the two separate attacks in the Danish capital.
Danish media reported the gunman to be Omar Abdel Hamid El-Hussein.
Reports 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.
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 Muhammad on a dog.
The gunman then moved on to a nearby synagogue where the guard, protecting a young girl's bat mitzvah ceremony, was gunned down.
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 neighbourhood and took away several people for questioning, witnesses said.
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."
Mr 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 the attacks.
The gunman's primary target was likely to have been the free speech event with Mr 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," Ms 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 Muhammad, images that led to sometimes fatal protests in the Muslim world.
Many Muslims consider any representation of the prophet blasphemous.
Mr Vilks sparked controversy himself in 2007 with drawings depicting Muhammad's head on a dog, triggering death threats.
He has lived under Swedish police protection since 2010 and two years ago a US woman was jailed for ten years in the United States for plotting to kill him.
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 escaped.
Mr Vilks sheltered on the floor of a cold room at the back of the cafe with one of the event's organisers.
Public forum planned for TCD
The recent attacks will be discussed at a public meeting in Trinity College Dublin tomorrow evening.
The Trinity Long Room Hub will host a discussion entitled 'After Charlie Hebdo: A Public Forum on Religion, Freedom and Human Rights'.
Professor John Horne, one of the speakers at the event, said it would be very different to the one held in the cafe in Copenhagen yesterday.
He said there would be nobody like Mr Vilks there.
Professor Horne said: "There will be three academic experts who are trying to bring their expertise so we can have a calm and rational public debate about these matters.
"The terrible nature of those events in Copenhagen, and it seems to me there were a copy cat of what happened in Paris around Charlie Hebdo and the Jewish supermarket, make it all the more urgent to hold the event."
Gardaí in Pearse Street will be liaising with the organisers of tomorrow's event and say it will be policed if required but they are not aware of any threats.