A police officer in Philadelphia was shot multiple times late Thursday by an attacker who later said he had pledged loyalty to the Islamic State, officials said Friday.

Authorities described the attack as a brazen assassination attempt that took place on a quiet West Philadelphia intersection and marveled that the officer was able to not only survive, but return fire and injure the shooter.

The attacker, who was firing a stolen police gun, was taken into police custody and “confessed to committing this cowardly act in the name of Islam,” Richard Ross Jr., the city’s new police commissioner, said at a news conference Friday.

Ross said that the attacker is a 30-year-old man from Yeadon, an area outside Philadelphia, who also has an address in the city. He was identified Friday as Edward Archer, and the office of Philadelphia’s district attorney said he had previously been arrested on charges of assault, threats and recklessly endangering someone.

After the attack Thursday night, police say Archer was speaking to detectives and made his declaration about the Islamic State, a militant group also known as ISIS and ISIL.

“He stated that he pledges his allegiance to Islamic State, he follows Allah and that is the reason he was called upon to do this,” Capt. James Clark said at the news conference. “He kept on echoing those sentiments and he wouldn’t give us anything more than that.”

The shooting in Philadelphia comes at a time of heightened fears about terrorism nationwide, occurring a little more than a month after a husband-and-wife pair in San Bernardino, Calif., killed 14 people in what was later deemed a terror attack. Shortly after the shooting, the female attacker there went on Facebook and said they both pledged allegiance to the Islamic State’s emir, Abu Bakr al-Baghdadi.

And hours before the shooting in Philadelphia, federal prosecutors announced that two Palestinian men who were born in Iraq and came to the United States as refugees had been charged as part of terrorism investigations.

Archer told Philadelphia police officials he believes they enforce laws that are contrary to the Koran, Ross said Friday.

Philadelphia Mayor Jim Kenney (D), who was sworn into office Monday, said Archer did “not represent” Islam and denounced the attacker’s invocation of the faith.

“This is a criminal with a stolen gun who tried to kill one of our officers,” Kenney said at the same news conference. “This has nothing to do with being a Muslim or following the Islamic faith.”

The FBI said Friday it is assisting the Philadelphia Police Department in the investigation, with the local police leading the probe.

As part of the effort, authorities were looking into trips Archer had taken to the Middle East. Archer traveled to Saudi Arabia in October and November of 2011 and went to Egypt for several months in 2012, an FBI spokesman said Friday night. Authorities will continue following up on this travel as part of the investigation, he said.

It was not clear how Archer obtained the gun, which was reported stolen from an officer’s home in 2013, Ross said.

“He certainly was targeting police,” said Ross, who became the city’s police commissioner Tuesday. “He wasn’t just targeting a motorist driving down the street … he was trying to assassinate this police officer.”‘

Ross said Archer fired 13 shots at Officer Jesse Hartnett, a uniformed officer who was driving his police car when the attack happened at about 11:40 p.m. Thursday. At one point, Archer got so close to the car that he had his gun “inside the car, firing at the officer,” Ross said.

Hartnett, 33, was hit three times in his left arm, which was broken and sustained “significant damage,” Ross said. He was listed in critical, but stable, condition at Presbyterian Hospital on Friday, authorities said.

At a news conference Friday, police showed a series of still surveillance images they said were captured at the intersection of South 60th and Spruce streets. Surveillance footage was also released showing the brief encounter.

In this footage, a man wearing a white robe over his clothing and what appears to be a mask is seen running through the crosswalk toward a police cruiser with his hand outstretched pointing a gun. He is then seen reaching to put the gun into the car’s window before fleeing.

“When you look at the video … this is absolutely one of the scariest things I’ve ever seen,” Ross said. “This guy tried to execute the police officer. The police officer had no idea he was coming.”

The footage then shows Hartnett quickly climbing out of the car and chasing after the other man, firing his gun with his outstretched right hand while his left arm remained at his side. Archer was taken into custody by other officers, Ross said.

“It’s amazing he’s alive,” Ross said of Hartnett. He added: “We’re just lucky, that’s all I can say. I can’t even believe he was able to survive this.”

Court records show that Archer had been charged with aggravated assault, terroristic threats and several other offenses for an incident in 2012. Last year, he pleaded guilty to charges of simple assault and carrying a firearm without a license.

Archer’s mother, Valerie Holliday, told the Philadelphia Inquirer that her son was a devout Muslim who had suffered head injuries.

“He’s been acting kind of strange lately,” she said. “He’s been talking to himself…He’s been hearing voices in his head. We asked him to get medical help.”

It is not clear what, if any, ties or exposure Archer may have had to the Islamic State. But the attack could fit a recent pattern of attacks that have seen people invoke the group, said J.M. Berger, an expert on terrorism and ­co-author of “ISIS: The State of Terror.”

“This is the model we’ve seen for ISIS over the last year or so,” Berger said Friday. “People, sometimes with very loose affiliation or no affiliation with the group, acting out in its name.”

Berger said that regardless of what comes out about Archer, the basic situation facing a country fearful of terrorism will remain the same.

“This is the same threat environment we’ve had for the last year,” he said. “These kinds of cases have become more common, and there’s every reason to expect they’re going to continue at an increased pace, regardless of what details come out on this, whether he is somebody who is mentally ill or was inspired on social media or has a more concrete connection.”

Gov. Tom Wolf (D) released a statement saying the shooting “is horrifying and has no place in Pennsylvania.”

“We are thankful that Officer Hartnett is alive and not facing life-threatening injuries after being ambushed,” Wolf said. “We wish him and his family the best during his recovery.”

Sen. Bob Casey (D-Pa.) said the shooting highlighted the danger faced by law enforcement officers nationwide.

“This was an act of barbarism and the accused must be held fully accountable,” Casey said in a statement. “Those who carry out attacks in the name of ISIS or any other terrorist organization must be fully prosecuted. This individual and any who would advocate similar acts are not representative of any religion — they are thugs, and criminals.”

A Philadelphia police officer was shot and killed last year when he interrupted a robbery at a video game store, becoming the city’s first officer fatally shot in the line of duty since 2012.

Law enforcement officers nationwide have been voicing increasing concerns about ambush attacks in recent years. In 2014, 15 officers were shot and killed in ambushes, more than any other type of shooting and matching 2012 for the most such deaths in a decade.

Police officers have described feeling anxious amid protests over how law enforcement uses deadly force, with current and former officers saying they feel heightened tension. But the concerns being expressed about ambushes predated the demonstrations that have cropped up nationwide.

A study released last year by the Department of Justice examining ambushes or planned attacks between 1990 and 2013 said that concerns about violence targeting officers were on the rise. That study also said that while the number of ambush attacks was steady over the period examined, the proportion of fatal attacks attributed to ambushes increased.

There were 52 officers killed in “felonious incidents” last year, according to the National Law Enforcement Memorial Fund, a nonprofit group that tracks line-of-duty deaths. That is down from 61 a year earlier. In addition, 73 officers died last year in “non-felonious incidents,” largely car crashes and job-related illnesses.

Last year, six officers were shot and killed in ambushes, the fund found. All told, the number of officers fatally shot is significantly down from the 1970s, when an average of 127 officers were shot and killed each year. Between 2000 and 2009, an average of 57 officers were fatally shot each year.

Further reading:

This story has been updated. First published: 9:12 a.m.