Two New York police officers have been shot dead by a gunman who then killed himself, officials said.

The officers were killed without warning on Saturday as they sat in their patrol car in Brooklyn, Police Commissioner William Bratton told a news conference, flanked by New York City Mayor Bill de Blasio.

"Although we're still learning the details, it's clear that this was an assassination, that these officers were shot execution style," said de Blasio.

Bratton named the gunman as Ismaaiyl Brinsley, 28, and said he fled into a nearby metro station after the shooting and died there from a self-inflicted gunshot wound to the head.

Police said Brinsley had shot and wounded his girlfriend in Baltimore before driving to Brooklyn.

Bratton added that the gunman had made online posts that were "very anti-police".

Anger against police

The shooting comes at a time when police in New York and nationwide are being heavily criticised for their tactics following the chokehold death of Eric Garner, an unarmed black man who was stopped by police for selling cigarettes illegally. 

Thousands rally against police violence in New York

US media reported a possible link between Brinsley, who was black, and anger over the death of Garner, based on a social media posting.

A post on Saturday on the site Instagram that appeared to be by Brinsley showed a silver pistol and said, "I'm Putting Wings On Pigs Today. They Take 1 Of Ours ... Let's Take 2 of Theirs."

The post included hashtags for Garner and for Michael Brown, an unarmed black teenager who was killed in August by a white police officer in Ferguson, Missouri. 

Hundreds of people protesting against police violence filled the Mall of America in Minnesota on Saturday, chanting "Black lives matter", causing part of the mall to shut down.

Demonstrators around the country have staged protests since a grand jury decided on December 3 not to indict the white officer involved in Garner's death, a decision that closely followed a Missouri grand jury's decision not to indict the officer who killed Brown.

Source: Agencies