A police officer was shot and killed early Wednesday while sitting in a police vehicle in the Bronx, in what the New York City police commissioner called an “unprovoked attack.” The gunman was killed, the police said.

The officer, Miosotis Familia, 48, a 12-year veteran of the police force assigned to the anti-crime unit of the 46th precinct, was taken to St. Barnabas Hospital, where she was pronounced dead.

Officer Familia was sitting in a marked Police Department vehicle near the corner of East 183rd Street and Creston Avenue, in the Fordham Heights neighborhood, around 12:30 a.m. She was nearing the end of her shift when a gunman fired a single round through the window, according to the police commissioner, James P. O’Neill. She was sitting in a mobile command unit, a vehicle that is big enough to house police equipment and several officers.

“Based on what we know right now, this was an unprovoked attack against police officers who want to keep this great city safe,” Commissioner O’Neill said at a news conference at the hospital. Outside, a line of officers saluted their fallen colleague; some escorted her body to the medical examiner’s officer.

Mayor Bill de Blasio, speaking at the same news conference, praised Officer Familia and expressed the city’s grief.

“She was on duty, serving this city, protecting people, doing what she believed in and doing the job she loved,” he said. “And after this shocking and sudden attack her fellow officers came to her aid immediately.”

Immediately after the shooting, Officer Familia’s partner called for assistance, and two other officers encountered the suspect, who was running on Morris Avenue, about one block away. When he drew a silver revolver, they opened fire, killing him.

The suspect was identified as Alexander Bonds, 34, whose last known address was in the Bronx. Mr. Bonds had been on parole for a robbery in the Syracuse area, the police said. Sgt. Brendan Ryan, a police spokesman, said there was no evidence that Mr. Bonds knew Officer Familia.

A bystander was struck by a bullet in that shootout, and is in stable condition, Commissioner O’Neill said.

The shooting was chillingly reminiscent of an ambush on Dec. 20, 2014, when two officers — Wenjian Liu and Rafael L. Ramos — sitting in a patrol car in the Bedford-Stuyvesant section of Brooklyn were fatally shot at point-blank range by a man who had traveled to the city from Baltimore vowing to kill officers. The man, Ismaaiyl Brinsley, who had a history of mental illness, then killed himself with the same gun.

Deadly attacks on the police received new attention when two officers were shot and killed in Des Moines and Urbandale, adjacent cities in Iowa, by a man who had ambushed them in separate attacks while they sat in their patrol cars. The gunman was sentenced to life in prison.

The most recent New York City police officer to be killed at the scene of a violent confrontation was Sgt. Paul J. Tuozzolo, who, with colleagues, confronted a suspect who had held his estranged wife and relatives hostage for several hours. That killing also occurred in the Bronx, about two miles from the site of the shooting on Wednesday.

Patrick J. Lynch, the head of the Patrolmen’s Benevolent Association, expressed outrage after the shooting of Officer Familia.

“This kind of violence against police officers cannot stand,” he said. “We need the public’s help. When you see someone that’s making threats, doing something against police officers, you need to let us know. You need to be our eyes and ears. We also have to remember the hundreds of police officers that are here now with their heads bowed in sorrow.”