Officer Jesse Hartnett was slowly patrolling his usual West Philadelphia beat just before midnight when a man appeared out of the darkness, firing a hail of bullets at close range as he charged toward the policeman's car.
Hours later, police say, Edward Archer confessed to shooting the officer and told investigators he was following Allah, and had pledged his allegiance to the Islamic State group. Archer said he believed the police department defends laws that are contrary to Islam, police said.
Local and federal authorities spent much of Friday trying to verify the motive and executing search warrants at two Philadelphia area properties associated with Archer, hoping for more insight into how and why the shooting happened.
Archer's mother told The Philadelphia Inquirer her 30-year-old son had been hearing voices recently and had felt targeted by police. She said the family had asked him to get help.
At a news conference, Philadelphia Police Commissioner Richard Ross, who just took office Tuesday, didn't label the shooting a terrorist attack, though he said Archer "clearly gave us a motive."
"It wasn't like laying it out completely, chapter and verse for us," Ross told reporters at the department's headquarters as Archer was being questioned upstairs. "We're left to say, 'OK, he's leaving a trail for us. Where's it going to lead us, if anywhere?'"
He said the gun used to shoot Harnett had been stolen from a fellow officer's home more than two years ago.
Investigators believe Archer traveled to Saudi Arabia in 2011 and to Egypt in 2012, FBI special agent Eric Ruona said, and the purpose of that travel was being investigated by the FBI. Police said there was no indication anyone else was involved, and it is unclear if and where Archer practiced his faith locally.
Archer's mother, Valerie Holliday, described her son as devout Muslim. Jacob Bender, the executive director of the Philadelphia chapter of the Council on American-Islamic Relations, an advocacy group, said he contacted about five inner-city mosques and found no one who knew of Archer.
At about 11:40 p.m. Thursday, Archer fired at least 13 shots toward Hartnett and eventually got up next to the car and reached through the driver's side window, investigators said. Despite being seriously wounded, Hartnett got out of his car, chased the suspect and returned fire, wounding his attacker in the buttocks, police said. Other officers chased Archer and apprehended him about a block away.
The 9 mm pistol used by Archer was recovered at the scene of the shooting, police said. It had been stolen from an officer's home in October 2013, investigators said. Officials said they were trying to figure out how Archer got the weapon and whether it passed through other people's hands since the theft.
Last March, Archer pleaded guilty to firearms and assault charges stemming from a 2012 case but was immediately released and placed on probation, court records show. Records also show he was scheduled to be sentenced Monday in suburban Philadelphia in a traffic and forgery case.
The attorney who represented him in the firearms case was unavailable for comment Friday because he was in court, his office said. His lawyer in the forgery case did not immediately return a message seeking comment.
Surveillance footage of the attack showed Archer dressed in a white, long-sleeved tunic. When asked if the robe was considered Muslim garb, Ross said he didn't know and didn't think it mattered.
"We've already established why he believes he did it, and that's probably enough," Ross said.
Hartnett, 33, was shot three times in the arm and will require multiple surgeries, but was listed in stable condition at a hospital. Archer was treated and released into police custody.
Ross repeatedly called Hartnett's survival "absolutely amazing." "It's nothing short of miraculous and we're thankful for that," he said.
The officer's father, Robert Hartnett, said his son was in good spirits. "He's a tough guy," he said.
Hartnett served in the Coast Guard and has been on the Philadelphia force for four years. He always wanted to be a police officer, his father said.
When Hartnett called in to report shots fired, he shouted into his police radio: "I'm bleeding heavily!"
Jim Kenney, in his first week as mayor of the nation's fifth-largest city, called Archer's actions "abhorrent" and "terrible" and said they have nothing to do with the teachings of Islam.
"This is a criminal with a stolen gun who tried to kill one of our officers," he said. "It has nothing to do with being a Muslim or following the Islamic faith."
The Christian Science Monitor reports that the National Fraternal Order of Police (FOP), an advocacy organization for police officers, has been characterizing recent lethal attacks on police as part of a broader national crisis, calling for hate crime protection for police officers.
"In the last few years, ambush attacks aimed to kill or injure law enforcement officers have risen dramatically," said FOP National President Chuck Canterbury, according to the New York Daily News. "All of theseofficers died because of the uniforms they were wearing."
But according to a website that keeps track of police officer deaths, Officer Down Memorial Page, line of duty deaths have actually declined in the last decade. Over the past 10 years, an average of 157 officers have been killed on the job. With one month left in the year, 2015 is on track to be the second least deadly year for police officers for decades.
Associated Press writers Michael R. Sisak and Kristen de Groot contributed to this report.