An armed suspect accused of wounding six officers in north Philadelphia during a nearly eight-hour standoff surrendered early Thursday, officials said.

The gunfire began Wednesday afternoon as police tried to serve an arrest warrant, police said. Shortly after midnight, police spokesman Sgt. Eric Gripp tweeted that a suspect was in custody and that SWAT officers were clearing the house.

Police on the scene said that the suspect surrendered without incident. The suspect was taken to a hospital for treatment because police had used tear gas, The Associated Press reported.

"It's nothing short of a miracle that we don’t have multiple officers killed today," Philadelphia Police Commissioner Richard Ross Jr. said Wednesday night.

Download the NBC News app for breaking news

Police were serving the warrant at the house in the northern part of the city when a gunman opened fire. Two officers were trapped upstairs for hours before they were safely freed along with three civilians by SWAT officers, the police commissioner said.

Police officers monitor activity near a residence while responding to a shooting on Aug. 14, 2019 in Philadelphia, PA. Mark Makela / Getty Images

In a press conference early Thursday, Ross said the suspect had prior arrests, but his office is not discussing the details of the charges at this time.

President Donald Trump in a tweet Thursday morning said the suspect "should never have been allowed to be on the streets" because of his "very dangerous criminal record."

Trump tweeted that the suspect, who was identified by an attorney as Maurice Hill, "looked like he was having a good time after his capture, and after wounding so many police."

"Long sentence — must get much tougher on street crime!" the president wrote.

All of the officers who were shot had been released from the hospital by late Wednesday night. The city’s mayor said that one of those officers, who is the father of two boys, suffered a graze wound to his head and could have been killed.

Police escort shooting suspect Maurice Hill into custody after a standoff that injured several police officers in Philadelphia on Aug. 15, 2019. Elizabeth Robertson / The Philadelphia Inquirer via AP

The attorney, Shaka Johnson, said in an interview after the arrest that Hill called him Wednesday evening, said he was barricaded in the house and that he "wanted to try to figure out a way out without any further violence coming to anyone or himself."

"I could sense that he was panicking and trying to figure out, 'How do I get out of this situation alive?'" Johnson said.

Johnson said he arrived at the home and was allowed to wear body armor and approach the home with officers, so the suspect would know Johnson was there before surrendering.

"I just kept the conversation as light-hearted as it could be under the circumstances, and when appropriate I switched back to: ‘You’ve got to come out, man, and you have to put your phone down. You can’t come out with any weapons, you’ve got to put your hands up,’” Johnson said.

A large police presence responded to the shooting at about 4:30 p.m. in a residential area of the Nicetown neighborhood.

Ross said that officers were already inside and in the rear of the building when the gunman opened fire, forcing them to escape a barrage of bullets through windows and doors.

"This was a narcotics warrant that went awry almost immediately, and the officers came under fire," Ross said. He said officers were in different areas of the building to quickly secure it, and some became trapped upstairs.

Officers took cover behind cars and blocked off surrounding streets as they were fired upon. Police returned fire, Ross said. Just about every officer at the scene came under fire at some point, he said.

SWAT team members were able to stealthily extract the two trapped officers and others, and police don't believe the gunman was aware of the operation that freed them, Ross said.

Police officers carrying rifles respond to a shooting in Philadelphia, Pennsylvania on Aug. 14, 2019. Mark Makela / Getty Images

Police have not said what kind of weapon or weapons the gunman was firing.

Philadelphia Mayor Jim Kenney said the suspect "was able to get these weapons, and a large magazine, a large amount of bullets." The mayor criticized federal and state governments for what he said was a lack of progress on gun control and an unwillingness to stand up to the National Rifle Association and others.

"So whether it’s our six officers that were shot or it's some 15-, 17-, 20-year-old kid on the streets of Philadelphia who gets shot with guns that shouldn’t be in people’s hands — it's aggravating, it's saddening, and it's just something we need to do something about," Kenney said. "Our officers deserve to be protected, and they don't deserve to be shot at by a guy for hours with an unlimited supply of weapons and an unlimited supply of bullets."

A witness who lives close to the shooting told NBC Philadelphia that she heard a series of shots, what she said felt like 100, and could smell what seemed to be gunpowder as the shootout unfolded. The unidentified woman told NBC Philadelphia that it felt "like a war."

The nearby campus for Temple University's Health Sciences Center was placed on lockdown as the situation unfolded, and Police evacuated area residences and businesses.

Yvonne Thomas-Curry, owner of Precious Babies Learning Academy, told NBC Philadelphia about 80 children were evacuated from the nearby day care. She said the children, who range from 6 weeks to 10 years old, were safe and warned parents to stay away until the area was cleared.

"We’re all pretty much together, we’re not scattered," Thomas-Curry said. "They’re all together now so we can closely monitor them and keep them away from the windows."

Lockdown for HSC campus has been lifted. All populations must avoid the incident scene and remain south of Venango St. Allied Security and Temple Police will assist. — Temple University (@TempleUniv) August 14, 2019

Pennsylvania Gov. Tom Wolf said in a statement that he and his wife, Frances, "are deeply troubled by another mass shooting in our commonwealth."

"Tonight is another reminder of the selfless sacrifice of our law enforcement officers and first responders. We are praying for a peaceful resolution and the full recovery of all those injured," Wolf said in the statement. "We must remain committed to combating violence and getting dangerous weapons out of our communities."

Johnson, the attorney, said Hill will likely be charged with six counts of attempted murder, aggravated assault and other counts.

Johnson thanked the Philadelphia Police Department and all the other agencies for their patience during the standoff, and said he is glad Hill will get his day in court rather than having the incident end in violence.

"This was a community under siege, and I'm glad everybody is able to return back to their homes without any further incident," Johnson said.