LONDON — The Copenhagen police said on Sunday that they had shot and killed a man believed to be behind two attacks that killed two people, one at a cafe and one outside a synagogue.

The first attack took place on Saturday, when a gunman sprayed bullets into the cafe where a Swedish cartoonist who had caricatured the Prophet Muhammad was speaking. Hours later, early Sunday, a man was shot outside the city’s main synagogue, according to the police.

One man was killed in the cafe attack and three police officers were wounded; a man was shot in the head in the second attack and later died, and two officers were wounded, Danish television reported. In each case the gunman escaped, raising fears throughout the city — the capital of Denmark — and setting off a police manhunt.

Prime Minister Helle Thorning-Schmidt called the shooting at the Krudttoenden cafe a terrorist attack and said that the nation was on high alert. “We feel certain now that it was a politically motivated attack, and thereby it was a terrorist attack,” Ms. Thorning-Schmidt said.