Officer Kozicki was struck three times — once in each upper thigh and once in the groin. Officer Levay was shot in the back, though his bulletproof vest protected him from serious injury; he fired seven bullets and killed the gunman. One subway passenger was grazed by a bullet, the police said.

Both officers were taken to Lutheran Medical Center in Brooklyn on Thursday night.

On Friday, just after noon, Officer Levay was released from the hospital. A detail of 60 police officers stood at attention, and a police band, including two bagpipers and a drummer, played. A nurse pushed him, in a wheelchair, out of the hospital, with his parents by his side. “We haven’t slept all night,” said Officer Levay’s father, Bob Levay.

“Right now,” he added, “I am so relieved it’s not even funny.”

Officer Levay waved, smiled and gave a thumbs-up to reporters. He walked the last few steps before climbing into an unmarked police car. Officer Kozicki was released from Lutheran a few hours later, the police said.

The shooting in Brooklyn came about an hour after an off-duty officer, Juan Pichardo, 34, was shot in the leg as he thwarted an armed robbery at a car dealership owned by his family on Boston Road in the Allerton section of the Bronx. Officer Pichardo was in stable condition on Friday night at Jacobi Medical Center. Four suspects have been arrested.

Patrick J. Lynch, president of the Patrolmen’s Benevolent Association, said the shooting in the Brooklyn subway station reflected the reality that people who violate basic rules and commit small crimes in subway stations — like turnstile jumpers — were more likely to take part in more serious crimes. For example, the police said, Mr. Jourdan was arrested and charged with fare evasion in September.