It is among the most dangerous, and most common, things police officers do: knocking on a door with a warrant, with no idea what might be waiting for them on the other side.

On Wednesday afternoon in a Philadelphia rowhouse, it was a man with a long criminal record and an AR-15 semiautomatic assault-style rifle. Bullets began to spray. Six officers were wounded, remarkably escaping with non-life-threatening injuries.

After a seven-hour standoff, which included a stealthy SWAT team rescue of two officers trapped inside the house, suspect Maurice Hill, 36, emerged amid a cloud of tear gas and gave himself up.

District Attorney Lawrence S. Krasner said Hill probably will face attempted murder, assault, firearms violations and other charges that could send him to prison for the rest of his life.

Krasner on Thursday credited “brilliant policing and maybe a little bit of a miracle” for the fact that no one died in the extended shootout and barricade situation — a sentiment echoed across Philadelphia on Thursday.

Sgt. Eric Gripp of the Philadelphia police wrote on Twitter: “6 officers shot — several more injured. A neighborhood terrified, and many lives forever changed. It seems strange to look back at yesterday as a ‘good’ day, but yet, here I am.”

Maurice Hill, a 36-year-old with a history of gun convictions, surrendered after shooting six police officers in a north Philadelphia neighborhood on Aug. 14. (Drea Cornejo, Jhaan Elker/The Washington Post)

Statistics suggest that Philadelphia was lucky to avert a disaster in Wednesday’s chaos. During the first half of the year, 60 law enforcement officers were killed, according to the National Law Enforcement Officers Memorial Fund, a nonprofit group that tracks line-of-duty deaths. Nearly half — 27 officers — died because of gunshots, including many doing seemingly common police work, such as serving felony arrest warrants, conducting a welfare check or responding to domestic disturbances.

Criminal justice experts emphasized how quickly such daily policing tasks can turn deadly.

“I would never call anything police do routine,” said Bill Humphrey, a former Dallas deputy police chief who was overseeing the department’s SWAT operations in 2016 when a gunman shot and killed five police officers there. “It seems routine to the public eye, maybe, but nothing ever should be routine. Because you can do 50 traffic stops and the 51st turns deadly.”

Richard Ross Jr., Philadelphia’s police commissioner, said Wednesday that it was “nothing short of a miracle that we don’t have multiple officers killed today. On Thursday, he added: “We were dealt a hand nobody should be dealt.”

Fallout from the shooting immediately became political and caught up in the debate about gun violence that has sparked anew since the recent mass shootings in El Paso and Dayton, Ohio. Though Dayton police intercepted and killed that mass shooter in less than a minute, he was still able to kill nine people and injure more than two dozen others with a burst of gunshots.

“Guns have flooded American cities, leading to senseless and preventable violence,” Philadelphia Mayor Jim Kenney (D) said Thursday.

Wednesday evening, an emotional Kenney stood in the rain and told reporters: “If the state and federal government doesn’t want to stand up to the NRA and some other folks, then let us police ourselves. But they preempt us on all kinds of gun control legislation.”

Kenney also said police officers need better protection from guns.

“They don’t deserve to be shot at by a guy for four hours with an unlimited supply of weapons and an unlimited supply of bullets,” he said. “It’s disgusting and we’ve got to do something about it.”

Krasner said Thursday that Hill has an extensive criminal record that stretches back nearly two decades. Court records for Hill showed a string of charges dating to 2001 for crimes including aggravated assault, reckless endangerment, burglary and driving under the influence, along with firearms and drug counts. Krasner said there were no new local criminal cases against Hill since he took office last year.

“It’s clear this man should not have been on the streets, in the sense that he obviously was a tremendous danger,” he said.

Krasner said the criminal justice system “did things that obviously did not stop this incident … What a lot of us do in law enforcement is risk management, and that there will be, like it or not, occasions when there are bad results.”

Hill also became a flash point in an increasingly rancorous debate in Philadelphia, and across the country, about more progressive policies toward prosecuting crime. Krasner, the city’s district attorney, is perhaps the most high-profile of the “progressive prosecutors,” a group of district attorneys who have won office in major cities promising to seek lighter sentences and to stop prosecuting low-level offenses.

William McSwain, the U.S. attorney in Philadelphia, blamed Krasner for a city climate of “stunning disrespect for law enforcement.”

“We have plenty of criminal laws in this city — but what we don’t have is robust enforcement by the district attorney,” McSwain said in a statement Thursday.

Krasner said in a statement that McSwain’s comments aimed to “detract from the great collaborative work of law enforcement last night … for his own political agenda and personal gain.”

Wednesday’s confrontation began when police arrived to serve a drug-related search warrant on a rowhouse in a north Philadelphia neighborhood. Officers who entered the house almost immediately came under fire, and the front-door surveillance camera of a neighbor across the street shows at least one wounded officer crawling out the front door onto the sidewalk.

Police then traded occasional fire with the suspect for hours, as the barricaded suspect allegedly shot at officers outside.

Annette Harrison, 59, said a local street is usually closed for most of the day as a “play street” for children. The day’s play was wrapping up when the shooting started.

“It was like the wild, wild West or the OK Corral,” she said.

The neighborhood is close knit, she said, with many activities geared around its children, such as a back-to-school event and a luau scheduled for later this month. The streets were closed off with police tape in all directions.

“Thank God no one was killed and those cops are okay,” she said.

Two officers who had initially entered the house remained trapped inside until about 9:30 p.m., when police announced that SWAT teams had “evacuated” them, along with other people who had been inside. Ross praised the two officers for warning others not to try to rescue them because Hill allegedly was so heavily armed.

“For a long time last night, I know our collective hearts were in our throats” because of the trapped officers, Ross said, declining to detail the strategy for the rescues because police might need to use the tactics again. “It wasn’t just one thing. There was a series of things that they did in order to ensure that the scene was as safe as possible for them, the hostages as well as for the suspect. When we told him repeatedly we wanted him to get out safely, we meant that.”

After the rescues, police worked to talk the suspect into surrendering rather than threatening to use force.

Chuck Wexler, executive director of the Police Executive Research Forum, said the officers showed “enormous restraint.”

“It looked like they had been trained well and they were under control,” he said. “There was a time in some departments where after police officers had been shot at, they would just go in and break down the door and go in.”

In a mark of how unusual the standoff was, both Ross and Krasner wound up on the phone with the suspect during negotiations.

Krasner said Shaka Johnson, an attorney who had represented the suspect, called his cellphone at about 9 p.m. to explain the situation and to connect the district attorney and the suspect. Krasner said the suspect was “in a very animated, excited, frankly dangerous state.” Krasner said that neither he or Johnson are hostage negotiators, but they “were doing what we could to try to lower the volume.”

Johnson said the suspect has a teenage son and a daughter born two days before the standoff.

“I think you’re going to find there is some level of emotional and mental disturbance,” Johnson said.

Johnson said the suspect eventually asked him to come to the house. He arrived at about 11:45 p.m. and used a megaphone to assure the suspect that he was there. “At some point he said he would come out,” Johnson said. “He said it very plainly: ‘I don’t want to die. I don’t want to end it this way.’ ”

Ross said he didn’t expect the situation to end peacefully. He said the suspect told him on a call that he did not want to go back to prison.

Shortly after midnight, police fired tear gas into the house, and the suspect walked out with his arms in the air. Live television captured police yelling: “Hands up! Hands up! Get down! Get down!”

When the suspect surrendered, Ross said, he still had a handgun in his pocket.

Natalie Pompilio and Maura Ewing, freelance journalists based in Philadelphia; and Reis Thebault, Michael Brice-Saddler, Timothy Bella and Maria Iati in Washington contributed to this report.