US President Barack Obama has condemned the killing of two New York police officers by a man who then turned his gun on himself.

"I unconditionally condemn today's murder of two police officers in New York City," Obama, who is on Christmas holiday in Hawaii, said on Sunday.

"Officers who serve and protect our communities risk their own safety for ours every single day and they deserve our respect and gratitude every single day."

The officers - Rafael Ramos, 40, and Wenjian Liu, 32 - were ambushed and fatally shot in their marked police patrol car by a 28-year-old man named Ismaaiyl Brinsley, said William Bratton, the commissioner for the New York Police Department.

Reports said Brisnley was apparently seeking revenge for the death of an unarmed black man during an arrest attempt.

Al Jazeera's John Terrett, reporting from New York, said Brinsley had posted "very vicious, anti-police messages" on social media before the attack, the first in which NYPD officers had been killed by gunfire since 2011.

Eric Holder, the US attorney general, promised the support of the Justice Department throughout the investigation. 

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

Bratton said after the attack Brinsley fled into a nearby metro station 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: Al Jazeera and agencies