A man with a gun was killed Thursday in a shootout with police in New York City after he pointed his weapon at a man in an apartment building elevator and a deli worker and threatened to shoot up a hospital, police said.

New York Police Department officers took the man to a hospital in a police vehicle and he was pronounced dead, Chief of Patrol Philip Rivera told reporters. Police officers were taken to a hospital for evaluation.

The man was in an elevator in a residential building in Manhattan’s Upper East Side around 7 p.m. when spoke to another man and then pulled out a firearm and pointed it at him, police said.

Police said the man then went to a nearby deli, pointed a gun at a worker behind the counter and told him to call 911, saying he was going to a hospital to shoot it up.

Shortly after, the man briefly entered Mount Sinai Medical Center before leaving and placing a firearm on the ground, police said. He then went back inside, where he told a a police officer working a paid security job that he had a gun, police said.

When the officer tried to escort him out, they struggled, the man retrieved his gun and the officer called for backup, police said.

The man then walked down the street and encountered police officers in vehicles. Rivera said the man immediately fired his gun at the officers when they got out of their vehicles. The officers returned fire and hit him, he said.

“Every day, our officers put on their uniforms and they encounter dangerous situations across this city. But it’s another kind of danger when someone goes into a deli and hospital with a gun and opens fire directly at the NYPD,” Rivera said.

Rivera didn't provide details about the man.