Police in Copenhagen killed a gunman early Sunday they believe was responsible for a pair of deadly attacks just hours earlier, the first at a cafe hosting a forum on free speech and the second outside a synagogue where a bar mitzvah was underway.

The killings, with their eerie echo of last month’s terrorist attacks in Paris, had sent Denmark’s capital into lockdown and had prompted a massive manhunt that extended across the border into Sweden. In all, the attacks left two people dead and five police officers injured.

At a news conference on Sunday morning, Danish Prime Minister Helle Thorning-Schmidt said the country had been “hit by terror.”

“We do not know the motive for the alleged perpetrator’s actions, but we know that there are forces that want to hurt Denmark,” the prime minister said, adding that they wanted to stifle Denmark’s freedom of speech.

Police said Sunday that they were confident that the man they fatally shot near a train station was the assailant in both attacks and that they had identified him using CCTV footage. Police were staking out a location associated with the suspect when a man approached and began shooting, prompting officers to return fire, a police official said.

“The culprit that was shot by the police task force at Norreport station is the person behind both of these assassinations,” Torben Molgaard Jensen, the chief police inspector, told reporters.

Survivors of the two attacks said they appeared to have been an attempt to mimic the Paris terrorist strikes, in which the staff of a satirical publication was massacred and four hostages were shot dead at a kosher supermarket.

The French ambassador to Denmark and a cartoonist — previously targeted for depicting the prophet Muhammad — were among those taking part in the debate at the cafe who survived the gunfire.

“It was the same intention as Charlie Hebdo, except they didn’t manage to get in,” the French ambassador, François Zimeray, told the news service Agence France-Presse, referring to the Jan. 7 attack in Paris on the satirical newspaper. “Intuitively, I would say there were at least 50 gunshots, and the police here are saying 200. Bullets went through the doors, and everyone threw themselves to the floor.”

Late into the night, police had been hunting for a lone gunman who fled in a getaway car and who had been described as a man in his late 20s wielding an assault rifle. A photo released by Danish authorities showed him wearing a dark-blue ski jacket with a red woolen cap and a matching scarf covering the lower portion of his face.

Police in Sweden, which is separated from Copenhagen by a five-mile-long bridge, also joined the search.

“We feel certain now that it was a politically motivated attack, and thereby it was a terrorist attack,” Thorning-Schmidt said in an appearance near the scene of the cafe shooting. She put the country on high alert, with warnings of a possible follow-up attack.

Just hours later, early on Sunday, police said one person was killed and two police officers were wounded in a shooting near a synagogue in downtown Copenhagen. The assailant fled on foot, police said.

Jewish community leaders said that a bar mitzvah service was underway inside the synagogue at the time of the attack and that the man who was killed was a young volunteer guard. Police protection had been stepped up at sensitive sites across the city, including synagogues, after the attack on the cafe.

Political leaders from across Europe condemned the attacks, with French Prime Minister Manuel Valls tweeting, “Freedom attacked in #Copenhagen. Solidarity with the Danes.”

Israeli Prime Minister Benjamin Netanyahu also condemned the attacks and called for European Jews to immigrate to Israel.

“The wave of attacks against Jews in Europe is expected to continue and it is up to us to stay prepared,” Netanyahu said, according to the Jerusalem Post. “Jews need protection wherever they are, but we’re telling you: Israel is your home.”

The attack was likely to add to already deep apprehensions over terrorism that are being felt across Europe as the continent contends with rising radicalism and a flood of homegrown fighters traveling to and from the battlefields of the Middle East.

Security services have said that they are overwhelmed by the scale of the threat, with an ever-growing number of possible suspects to surveil and potential targets to protect on a continent unaccustomed to regular bursts of extremist violence in its biggest cities.

The target of Saturday’s earlier attack was a north Copenhagen cafe, the Krudttonden, that is well-known for its jazz performances. On Saturday afternoon, it was hosting a community discussion titled “Art, Blasphemy and the Freedom of Expression.”

Among the organizers was Swedish cartoonist Lars Vilks, who has received death threats for his depiction of the prophet Muhammad with the body of a dog in 2007. An al-Qaeda faction placed a bounty on his head, and an American woman calling herself Jihad Jane was sentenced in 2014 to 10 years in prison for plotting to kill Vilks.

In recent years, Vilks, 68, has had constant police protection.

Vilks had security guards with him at the cafe on Saturday and was unharmed in the attack. The French ambassador was also unhurt by the hail of bullets, which left dozens of jagged holes in the cafe’s plate-glass windows.

Police did not release the identity of the man who was killed, who was said to be a civilian.

Last month’s attack on Charlie Hebdo, in which editors and cartoonists were among 12 people killed, was thought to have been motivated by the magazine’s depictions of Muhammad. Twenty people in all were killed over three days of violence in Paris, including three assailants, all of whom grew up in France.

Police said the assailant in the Copenhagen cafe attack spoke Danish. He unleashed his fusillade of gunfire in the middle of the afternoon, with dozens of people gathered to hear Vilks, Zimeray and others discuss the limits of free expression in the age of terrorism.

Inna Shevchenko, an activist with the feminist group Femen, said she was in the middle of a speech, telling the audience that “often it is an illusion that we have freedom of speech in Europe,” according to tweets she later sent.

“Then we heard shots.”

In audio of the moment the gunman struck that was posted online by the BBC, a woman can be heard speaking before she is interrupted by a volley of gunfire.

“I realize that every time we talk about the activity of those people there will be always, ‘Yes, it is freedom of speech, but,’ ” the woman says. “And the turning point is ‘but.’ Why do we still say ‘but’ when we . . . ”

The shots are steady and sustained. From inside the cafe, the sounds are of chairs sliding along the floor as people dive for cover. No one screams.

Survivors of the attack said police who had been standing guard outside the cafe returned fire. The three injured officers were apparently among those who had been standing guard.

Inside the cafe, survivors said, the reactionwas remarkably calm.

“We could not get away, so we continued our discussion,” Helle Merete Brix, one of the organizers, told Denmark’s TV2 News.

The attack at Charlie Hebdo has ignited passionate debate across Europe over the right of free expression and whether speech that insults a particular religion or group should be protected. The latest iteration of the debate has roots in Denmark, where in 2005 the newspaper Jyllands-Posten set off a global furor after publishing cartoons of Muhammad, an act that many Muslims consider blasphemous.

Vilks has been an outspoken advocate for free expression. The Lars Vilks Committee, established to support him, gave its 2014 prize to Charlie Hebdo, just three months before the magazine’s staff was massacred.

Saturday’s event was timed to mark the anniversary of a religious edict against the author Salman Rushdie, who was threatened with death by Islamist extremists and lived in hiding for years after the publication of his novel “The Satanic Verses.”