A gunman shot dead two New York City police officers and then killed himself, according to police, after a social media post indicated he may have been seeking revenge for the death of an unarmed black man during an arrest attempt.

The two officers, Rafael Ramos, 40, and Wenjian Liu, 32, were ambushed in their patrol car on Saturday afternoon, said NYPD Police Commissioner William Bratton.

"They were quite simply assassinated, targeted for their uniforms," he told a news conference after the attack, the first in which NYPD officers had been killed by gunfire since 2011.

The two men were attacked outside a housing project in the Bedford-Stuyvesant section of Brooklyn at a tense time for the NYPD, the largest police force in the country.

Protests over policing tactics have roiled the city since a grand jury declined this month to indict a white NYPD officer in the killing of Eric Garner, a black man who died after being placed in a chokehold during a July arrest on Staten Island.

The shooter fired through the passenger-side window of their marked patrol car, striking both officers in the head before they had a chance to respond, Mr Bratton said. The suspect fled on foot, followed by other police, then took his own life on a subway platform.

Mr Bratton said the gunman, identified as 28-year-old Ismaaiyl Brinsley had made "anti-police" comments online.

US President Barack Obama condemned the killings, saying "two brave men won't be going home to their loved ones tonight." US Attorney General Eric Holder promised the support of the Justice Department throughout the investigation.

President Obama called New York City Police Commissioner William Bratton today to express his condolences for the Saturday’s killings and offered any assistance needed, according to White House spokesman, Eric Schultz.

The US president said his staff will continue to monitor the situation in New York.    

Shortly before the double shooting, a message on an Instagram account apparently belonging to Mr Brinsley said "They Take 1 Of Ours ... Let's Take 2of Theirs."

The posting was followed with hashtags referencing Garner and Michael Brown, an unarmed black teenager shot and killed by a white officer in Ferguson, Missouri in August.

Mr Bratton said investigators were checking whether Brinsley had attended any of the recent protests.

The killings of Rafael Ramos and Wenjian Liu also revealed bitter anger among some police toward New York City Mayor Bill de Blasio, who they see as not being supportive in the face of public anger.

Several officers turned their backs on de Blasio when he arrived at the Brooklyn hospital where the two officers were taken after they were shot, video showed.

It was unclear why the gunman chose Brooklyn.

Authorities said Mr Brinsley, who previously lived in Georgia, had shot and wounded his girlfriend in Baltimore early on Saturday morning before heading north to New York City.

Baltimore County Police said in a news release officers had learned of the Instagram threat and contacted the NYPD by telephone about 30 minutes before the shooting, following that warning with a faxed photo of Mr Brinsley.