SYDNEY — The deadly siege began in the most incongruous of ways, on a sunny Monday morning inside a cheerful cafe in the heart of Australia’s largest city. An Iranian-born gunman burst in, took 17 workers and customers hostage, and forced some to hold a flag with an Islamic declaration of faith above the shop window’s festive inscription of “Merry Christmas.”

It ended after midnight with a barrage of gunfire that left two hostages and the gunman dead, four others wounded, and a nation that has long prided itself on its peace rocked to its core.

After waiting 16 hours, police decided to storm the Lindt Chocolat Cafe early Tuesday when they heard a number of gunshots from inside, said New South Wales state police Commissioner Andrew Scipione.

A loud bang rang out, several hostages ran from the building and police swooped in amid heavy gunfire, shouts and flashes. A police bomb disposal robot also was sent into the building, but no explosives were found.

“They made the call because they believed that at that time, if they didn’t enter, there would have been many more lives lost,” Scipione said.

The gunman was identified as 50-year-old Man Haron Monis, who once was prosecuted for sending offensive letters to families of Australian troops killed in Afghanistan.

Prime Minister Tony Abbott said he had “a long history of violent crime, infatuation with extremism and mental instability.”

Scipione wouldn’t say whether the two hostages who were killed — a 34-year-old man and a 38-year-old woman — were caught in crossfire, or shot by their captor. Among the four wounded was a police officer shot in the face.

“Until we were involved in this emergency action, we believe that no one had been injured. That changed. We changed our tactic,” he said.

An investigation into the incident was underway.

Officials rolled one gurney out of the cafe carrying what appeared to be a man draped in a blood-soaked sheet with a bloody handprint in the center. Paramedics also carried away a woman with blood-covered feet.

“I can only imagine the terror that they’ve been through,” Scipione said. “They are very brave people who in many cases were just buying a cup of coffee and they got caught up in this dreadful affair. We should reflect on their courage.”

The prime minister also reflected on how an ordinary day turned terrifying.

“There is nothing more Australian than dropping in at the local cafe for a morning coffee, and it’s tragic beyond words that people going about their everyday business should have been caught up in such a horrific incident,” Abbott said.

While Monis’ motivation for the attack was still unclear, Abbott confirmed he was “well-known” to state and federal authorities.

Last year, Monis was convicted and sentenced to 300 hours of community service for using the postal service to send what a judge called “grossly offensive” letters to families of soldiers killed in Afghanistan between 2007 and 2009.

At the time, Monis said his letters were “flowers of advice,” adding: “Always, I stand behind my beliefs.”

Monis later was charged with being an accessory to the murder of his ex-wife. Earlier this year, he was charged with the sexual assault of a woman in 2002. He has been out on bail on the charges.

“He had a long history of violent crime, infatuation with extremism and mental instability,” Abbott said. “As the siege unfolded yesterday, he sought to cloak his actions with the symbolism of the ISIL death cult. Tragically, there are people in our community ready to engage in politically motivated violence.”

“This is a one-off random individual. It’s not a concerted terrorism event or act. It’s a damaged-goods individual who’s done something outrageous,” his former lawyer, Manny Conditsis, told Australian Broadcasting Corp.

“His ideology is just so strong and so powerful that it clouds his vision for common sense and objectiveness,” Conditsis said.

Flags were lowered to half-staff on the landmark Harbour Bridge as Australians awakened to the surreal conclusion of the crisis. Premier Mike Baird expressed disbelief that the attack could happen in Australia — a place he dubbed “a peaceful, harmonious society which is the envy of the world.”

The siege began about 9:45 a.m. in Martin Place, a plaza in Sydney’s financial and shopping district that is packed with holiday shoppers this time of year. Many of those inside the cafe would have been taken captive as they stopped in for their morning coffees.

Hundreds of police flooded the city. Streets were closed and offices evacuated. The public was told to stay away from Martin Place, site of the state premier’s office, the Reserve Bank of Australia, and the headquarters of two of the nation’s largest banks. The state parliament house is a few blocks away, and the famous Sydney Opera House also is nearby.

Throughout the day, several people were seen with their arms in the air and hands pressed against the window of the cafe, with two people holding up a black flag with the Shahada, or Islamic declaration of faith, written on it.

Channel 10 news said it received a video in which a hostage in the cafe had relayed the gunman’s demands. The station said police requested they not broadcast it, and Scipione separately asked media that might be contacted by the gunman to urge him instead to talk to police.

Australian Muslim groups condemned the hostage-taking in a joint statement and said the flag’s inscription was a “testimony of faith that has been misappropriated by misguided individuals.”

Australia’s government raised the country’s terror warning level in September in response to the domestic threat posed by supporters of the Islamic State group, also known as ISIL. Counterterror law enforcement teams later conducted dozens of raids and made several arrests in Australia’s three largest cities — Melbourne, Sydney and Brisbane.

Outbrain