The police deaths in Brooklyn followed weeks of protests in New York and other parts of the United States after separate cases in which grand juries declined to charge two white police officers involved in the deaths of two unarmed black men.
Quoting a law enforcement source, NBC's New York affiliate reported that the gunman may have been seeking retribution against police after posting a threatening message on a social media account.
The New York Times said the slain officers had been patrolling in Brooklyn's Bedford-Stuyvesant section of the borough. NYPD Deputy Chief Kim Royster said it appeared the shots struck them in the upper body, the Times reported.
It said the gunman opened fire on the officers from the patrol car's passenger side and fled into a nearby subway station. The man then died from a self-inflicted gunshot wound to the head, Royster told the newspaper.
A police spokesman could not confirm the newspaper account, but he said the officers had been taken to Brooklyn's Woodhull Medical Center and their conditions were unknown.
The spokesman identified the shooter as a 28-year-old man and said he had been taken to Brooklyn Hospital Center. A weapon has been recovered, he said.
New York police have come under intense pressure in recent weeks, with protests erupting after a grand jury declined to charge an officer involved in the chokehold death of Eric Garner during an arrest attempt in the borough of Staten Island.
Mike Isaac, a neighborhood resident, told CNN that Saturday's shooting took place in a largely African-American neighborhood that and had been tense since the protests over Garner's death. "The mood is pretty freaked out," he said.
Police set up a perimeter for several blocks around the street corner where the shooting occurred. Only residents were allowed to cross the police line and the subway line where the gunman reportedly shot himself was shut down.
"I just feel sorry for the policemen that got killed," said neighborhood resident Olin Barton, a 78-year-old retiree.
"I don't think there's a relationship with Garner, I doubt so anyway. It must have been someone mentally ill."
The grand jury's decision this month on the officer involved in Garner's death followed widespread demonstrations in the United States over a grand jury decision last month not to indict a police officer who shot and killed Michael Brown, a teenager in Ferguson, Missouri.
The protests have continued sporadically - on Saturday demonstrators against police violence shut down part of the Mall of America in Minnesota on Saturday, one of the nation's largest shopping centers, a community group said.
The Times said the last fatal shooting of a New York City police officer was in 2011.
The Federal Bureau of Investigation reported last month that 76 law enforcement officers in the United States died last year while on duty, including 27 during criminal acts, a sharp drop from 2012.
CONDEMNATIONS OF SHOOTINGS
The Rev. Al Sharpton, a New York civil rights leader who has supported the families of Brown and Garner, said he was outraged by the officers' killings, if they were related to the deaths of Garner and Brown.
"Any use of the names of Eric Garner and Michael Brown, in connection with any violence or killing of police, is reprehensible and against the pursuit of justice in both cases," he said in a statement.
In Los Angeles, Urban Policy Roundtable President Earl Ofari Hutchinson and other civil rights leaders also condemned the shootings in a statement.