A New York City police officer was randomly attacked and stabbed in the neck late Wednesday while patrolling in Brooklyn, which resulted in a struggle that caused two additional officers to suffer gunshot wounds.

Police said at an early news conference Thursday that a preliminary investigation indicated a male suspect walked up to the officers casually and whipped out the knife. They said the officers involved had been assigned to an anti-looting post to prevent people from breaking into stores.

CARDINAL DOLAN RESPONDS TO VANDALISM AT ST. PATRICK'S CATHEDRAL

"That officer was stabbed in the left side of his neck, thank God, missing an artery," New York City Police Commissioner Dermot Shea said.

CARDINAL DOLAN RESPONDS TO VANDALISM AT ST. PATRICK'S CATHEDRAL

He called it a "completely cowardly, despicable, unprovoked attack on a defenseless police officer."

Two additional officers were struck in the hand by gunfire during the incident, police said. They were rushed to Kings County Hospital and are expected to survive.

Nearby officers later heard the gunshots and responded to find the suspect brandishing a gun, which police said was likely taken from an officer. The suspect was shot multiple times at the scene later described by Shea as "chaotic." He added that 22 shell casings were recovered following the incident.

"I think we are all fed up with what we're seeing and many levels. This violence has to stop," Shea said.

The FBI Joint Terrorism Task Force is also aiding in the investigation.

New York City has been trying to get a handle on the widespread unrest after the death of George Floyd in Minneapolis police custody. The shooting occurred nearly four hours after the city's 8 p.m. curfew.

PROTESTERS IN BROOKLYN DEFEND TARGET STORE FROM LOOTING, RIOTS

New York City police officers watch protesters calling for justice over the death of George Floyd, Wednesday, June 3, 2020, in the Brooklyn borough of New York. Floyd died after being restrained by Minneapolis police officers on May 25. (AP Photo/Frank Franklin II)

New York City police officers watch protesters calling for justice over the death of George Floyd, Wednesday, June 3, 2020, in the Brooklyn borough of New York. Floyd died after being restrained by Minneapolis police officers on May 25. (AP Photo/Frank Franklin II)

NYC MAN WITH WOLVERINE-TYPE CLAWS APPARENTLY THREATENED GEORGE FLOYD PROTESTERS, POLICE SAY

Mayor Bill de Blasio went to the hospital after the confrontation to check on the officers, according to a spokesperson. He later spoke at the press conference on Thursday.

"Thank God all our officers will recover," de Blasio said. "It's another example of what it means every day for men and women of the NYPD to protect all of us."

Video purportedly of the incident on Twitter showed streets filled with police cars at the scene, while multiple gunshots could be heard.

"No matter what else is happening around us, we've got to be there for each other,"  the mayor added. "Officers protect us. We have to respect, support, protect them. We've got to find a way to move forward no matter how much is thrown at us. The coronavirus and everything else."

New York City has been roiled by days of protests over police brutality. It didn't appear that the unprovoked incident was related to the demonstrations. There were peaceful marches and protests throughout the day Wednesday, but police moved in to break them up when the curfew took effect.

GEORGE FLOYD UNREST: POLICE OUT IN FORCE IN MAJOR CITIES TO TAMP DOWN ON RIOTS AS CURFEWS TAKE EFFECT

Several large marches in other parts of Brooklyn had continued after the curfew that authorities imposed to stop stores from being damaged and ransacked.

The incident comes after a shocking video posted on social media Tuesday showed a New York City police officer being struck by a vehicle in what appeared to be a deliberate hit-and-run. Shea said the officer was still in intensive care, "but recovering slowly."

"We have to find a way to come together and move forward," de Blasio said. "Tonight is a story of bravery, courage, and thank god the story ends with a day soon when officers will leave this hospital and go home to their families."

An investigation into the Brooklyn incident is ongoing. Shea said an update will be provided later today.

He didn't speculate on the alleged stabber's motive, who was not identified, but Police Benevolent Association President Patrick Lynch blamed anti-police rhetoric during the protests.

CLICK HERE TO GET THE FOX NEWS APP

“Are we surprised? Are we surprised we’re here in the hospital again? Did we doubt because of the rhetoric we’re hearing, the anti-police rhetoric that’s storming our streets, are we surprised that we got this call? I’m not. We said it’s going to happen," he said.

Fox News' Dom Calicchio and the Associated Press contributed to this report