New York City Mayor Bill de Blasio and his wife, Chirlane McCray, visit a makeshift memorial near the site where New York Police Department officers Rafael Ramos and Wenjian Liu were shot and killed in Brooklyn.

Dec. 23, 2014 New York City Mayor Bill de Blasio and his wife, Chirlane McCray, visit a makeshift memorial near the site where New York Police Department officers Rafael Ramos and Wenjian Liu were shot and killed in Brooklyn. Craig Ruttle/AP

Police say Ismaaiyl Brinsley ambushed the officers, fatally shooting them before killing himself inside a subway station.

The unidentified assailant was pursued into a nearby subway station, where he killed himself, police said.

The unidentified assailant was pursued into a nearby subway station, where he killed himself, police said.

A gunman shot and killed two New York City police officers before taking his own life in a brazen ambush that played out on a quiet Brooklyn street corner Saturday afternoon, New York police said.

Officers Wenjian Liu and Rafael Ramos were shot at point-blank range while sitting beside one another in a police car in the East Flatbush neighborhood of Brooklyn, police said.

“It’s clear that this was an assassination,” New York City Mayor Bill de Blasio said at a news conference Saturday night. “These officers were shot execution-style, a particularly despicable act which goes to the heart of our society and our democracy.”

“It is an attack on all of us,” he added.

Ramos and Liu, who were shot in the head, were transported to Brooklyn’s Woodhull Medical Center, where they were later pronounced dead, according to New York Police Commissioner William J. Bratton, who also spoke at the news conference Saturday evening.

The shooting has heightened tensions in a country and city already roiled by demonstrations against police brutality in the wake of recent killings of unarmed black men by white police officers in Missouri and New York.

Just three hours before the shootings, the gunman — identified by police as 28-year-old Ismaaiyl Brinsley — declared his intention on his Instagram account to kill police officers as retribution for the recent police killings of Michael Brown and Eric Garner.

Before he made his way to Brooklyn on Saturday morning, police said, Brinsley shot his girlfriend about 5:45 a.m. in Owings Mills, Md., about 15 miles outside Baltimore. The shooting occurred at a gated apartment complex called Greenwich Place Town Center, adjacent to a parking lot for a movie theater at Owings Mills Mall.

Late Saturday night, Baltimore County police confirmed that Brinsley was the gunman in the earlier shooting that wounded the 29-year-old Owings Mills woman. Police said the victim, whose name has not been released, was shot in the abdomen.

Police said Brinsley fled the scene of the shooting before they arrived. By 1:30 p.m., police said they became aware of Instagram posts by the suspect that included “threats against police officers.” Authorities said they quickly traced the posts and the phone Brinsley was using to a location in Brooklyn.

About 2:10 p.m., Baltimore County police called New York’s 70th Precinct to discuss the threatening Instagram posts and informed officers that Brinsley’s phone was “pinging” at a location in their precinct. At the same time, Baltimore County authorities said they faxed a “wanted” poster to the NYPD with information about Brinsley. Around 2:50 p.m., they sent a teletype with the same information in the flier to NYPD’s “real-time crime center.”

They were too late. At about 2:45 p.m., police said, Brinsley approached the officers’ marked NYPD patrol car outside the Tompkins Houses in the Bedford-Stuyvesant area of Brooklyn and began firing into the front passenger-side window, striking both officers in the head.

Officers Wenjian Liu, left, and Rafael Ramos were killed Saturday in Brooklyn. The suspect ran to a subway station and killed himself, police said. (New York Police Department/AP)

Brinsley’s girlfriend was shot in the stomach and underwent surgery, according to a New York Daily News article. She was expected to recover.

“I’m putting Wings on Pigs Today,” Brinsley wrote on his Instagram account beneath a photo of a silver-colored semiautomatic pistol and a pair of blood-stained camouflage pants. “They TAKE 1 Of Ours . . . Lets Take 2 of Theirs.” The Instagram photos use hashtags for Brown and Garner and appear to indicate that the shootings were an act of revenge.

Brinsley stole his girlfriend’s phone after the shooting before traveling to New York. At some point along the way, he posted a menacing update on Facebook.

“I Always Wanted To Be Known For Doing Something Right . . . But My Past Is Stalking Me and My Present Is Haunting Me,” it read.

Bratton said investigators were looking into all of Brinsley’s ­social-media accounts in an attempt to unravel the shooter’s motivations.

“Some of the postings, which I understand are out there, would seem to indicate that he had a very strong bias against police officers,” he said.

Bratton said investigators have no reason to believe Brinsley has any connection to terrorist groups.

“We’re not seeing a connectivity to any organized entity at this time,” he noted.

A law enforcement official said the gunman started “pumping bullets” into the officer’s car, the New York Post reported.

“The perp came out of the houses, walked up behind the car and lit them up,” a police official told the Daily News.

After the shooting, Brinsley ran up the street and was pursued by other officers into a subway station, where he was found with a gunshot wound that appeared to be self-inflicted, police said. He was declared dead at a nearby hospital, police said.

In dozens of cities across the country in recent weeks, protesters have organized marches, staged die-ins, blocked rush-hour traffic and clashed with police using rallying cries tied to the killings of Brown and Garner — “Hands up, don’t shoot,” in Brown’s case, and “I can’t breathe,” in reference to Garner’s last words.

Much of those protests have been documented, if not fueled, by up-to-the-minute social media accounts, which have provided a virtual spark and rallying point for like-minded protesters aggressively seeking change.

New York City has served as the most prominent staging ground for many of the demonstrations, with protests happening almost nightly since a Ferguson grand jury declined to indict Ferguson police officer Darren Wilson on Nov. 24 in Brown’s shooting death.

On Saturday, Bratton said those demonstrations had taken their toll on New York City’s police force.

Last week, New York police arrested three men who are accused of assaulting two police lieutenants on the Brooklyn Bridge at the end of a march protesting the Eric Garner grand jury decision.

At the news conference Saturday night, Bratton said the day’s shootings would only add to the growing feeling that New York police officers are under attack, but he said he expected them to endure.

“Today, two of New York’s finest were shot and killed with no warning, no provocation,” he said. “They were, quite simply, assassinated — targeted for their uniform and for the responsibility they embraced to keep the people of this city safe.”

In a statement, President Obama said, “I unconditionally condemn today’s murder of two police officers in New York City. Two brave men won’t be going home to their loved ones tonight, and for that, there is no justification. The 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,” Obama said. “Tonight, I ask people to reject violence and words that harm, and turn to words that heal — prayer, patient dialogue, and sympathy for the friends and family of the fallen.”

Attorney General Eric H. Holder Jr. also condemned the shooting, calling it “an unspeakable act of barbarism. I was deeply saddened to hear of the loss of these two brave officers in the line duty.

“This cowardly attack underscores the dangers that are routinely faced by those who protect and serve their fellow citizens,” Holder said in a statement. “As a nation we must not forget this as we discuss the events of the recent past.”

Through his National Action Network, the Rev. Al Sharpton released a statement several hours after the shooting condemning the use of violence as a means of affecting change.

“I have spoken to the Garner family and we are outraged by the early reports of the police killed in Brooklyn today,” Sharpton said in the statement. “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.”

In a statement, the family of Michael Brown condemned the “senseless killing” of the officers.

“We reject any kind of violence directed toward members of law enforcement,” read the statement, issued by family attorney Benjamin Crump. “It cannot be tolerated. We must work together to bring peace to our communities. Our thoughts and prayers go out to the officers’ families during this incredibly difficult time.”

Some took aim at Holder, de Blasio and other government officials for what they view as anti-police statements in the wake of the Garner and Brown killings and judicial decisions.

“Unfortunately, I don’t believe anyone connected to law enforcement is surprised this happened,” said Gary McLhinney, a former Baltimore police union president and former chief of the Maryland Transportation Authority police, who is now a labor negotiator for police unions. “Political rhetoric and lies have consequences. When our leaders make statements that encourage lawlessness and demean an entire profession, this is the result.”

And former New York governor George Pataki, responding to the shootings via Twitter, said that he was “sickened by these barbaric acts, which sadly are a predictable outcome of divisive anti-cop rhetoric” of Holder and de Blasio.

President Obama, who was on the first day of a two-week family vacation in Hawaii, was briefed on the Brooklyn police shooting by aides, the White House said.

J. Freedom du Lac, Peter Hermann, Ed O’Keefe and David Nakamura contributed to this report.