Philadelphia police say the man who sparked a vicious gun battle with cops on Wednesday that left six officers injured has been taken into custody after a tense, hours-long standoff.

The arrest came nearly eight hours after the first gunshots rang out in the Nicetown-Tioga neighborhood when narcotics officers attempted to serve a warrant and, in the words of Philadelphia Police Commissioner Richard Ross, things “went awry almost immediately.”

Gunfire erupted when officers were already inside the home with the warrant and the ensuing shootout was so intense that some officers “had to escape through windows and doors” to avoid the “barrage of bullets,” Ross said.

Two Philadelphia police officers and three people they had taken into custody before the shooting became trapped inside the home with the gunman for several hours as he repeatedly fired on law enforcement responding to the scene, police said. From his location on the first floor during the standoff, the gunman reportedly fired up into the ceiling at the officers on the second floor, but they were uninjured and safely evacuated by a SWAT team late Wednesday.

“ It’s nothing short of a miracle that we don’t have multiple officers killed today,” Ross said at an evening press conference.

The six officers wounded in the initial shooting were treated at local hospitals for non-life threatening injuries and released late Wednesday night, police said.

While Philadelphia officials expressed relief that no one had been killed amid the chaos, Mayor Jim Kenney took the opportunity to issue a plea: “Our officers need help. They need help. They need help with gun control. They need help with keeping these weapons out of these people's hands,” Kenney said, adding that the two young sons of one of the injured police officers nearly lost their father “because this government … don’t want to do anything about getting these guns off the streets.”

Police have yet to identify the suspect, and it wasn’t immediately clear what kind of weapons he is alleged to have fired at officers. The Bureau of Alcohol, Tobacco, Firearms and Explosives sent its agents to the scene to assist local police.

Residents who witnessed the initial gun battle said it thrust the entire neighborhood into chaos.

Dale Whittaker, a witness of the early gunfire, told a local NBC affiliate that he saw a gunman shooting from a second story window. “He started shooting out and everyone started running,” Whittaker said. "...Every officer ran. It was really serious."

“This is daytime," a neighborhood woman who identified herself as Beverly told the local NBC affiliate. “Children are running around playing. Where they are was a play lot, where the kids are."

Two nearby day-care centers were evacuated during the standoff, with children as young as 6-months-old safely moved onto two buses to be reunited with their families.

"It was like a war—like a scene that you see in war," a neighborhood woman told NBC 10. “The guns, the fire, the noise—it was like bombs going off simultaneously at a time where people are having dinner.”

-- Christopher Moraff contributed reporting