A combination of high-tech police work, social media and a chance encounter on a New Jersey street led to the arrest Monday of Chelsea bombing suspect Ahmad Khan Rahami after a shootout with police, law enforcement officials said.

The breakneck speed with which Rahami, 28, was finally captured — just 50 hours after the blast in Manhattan’s Chelsea neighborhood Saturday night as well as one he is believed to have set earlier that day in Seaside Park, New Jersey — capped a feverish two-day investigation by the federal Joint Terrorism Task Force which employed numerous levels of technology, as well as cops pounding the pavement.

“A lot of technology was involved in this but [also] a lot of good old-fashioned police work between the FBI, NYPD and members of the Joint Terrorism Task Force,” new NYPD Commissioner James O’Neill said at a news conference Monday in Manhattan.

Assistant director in charge William F. Sweeney Jr., who heads the FBI office in Manhattan, said several hundred law enforcement officials were involved in the case.

“Based on our evidence collection and other analysis,” he said, the JTTF quickly focused on Rahami, as well as a car carrying five people Sunday night that was stopped as it traveled into Brooklyn from New Jersey and Staten Island. The people in the car were questioned for several hours in Manhattan and finally released without being arrested, Sweeney said. He wouldn’t elaborate on their statements, but Rep. Peter King (R-Seaford) said the five people were Rahami’s family. “They were family members who left the house last night,” King said. “They gave very significant information to the FBI.”

Sweeney declined to disclose what law enforcement techniques were used. But based on his emphasis on “evidence collection” and what he said was “other analysis,” it appeared that the Chelsea bomb on West 23rd Street, the unexploded device found by a photographer on West 27th Street with a cellphone and the device that detonated in Seaside Park provided either DNA or fingerprint evidence leading investigators to Rahami, said another law enforcement official who declined to be named. King said the malfunctioned device allowed investigators to find a fingerprint and the mobile phone that they traced to Rahami.

advertisement | advertise on newsday

With Rahami identified as a prime suspect, officials resurrected a photograph of him. After JTTF investigators searched locations linked to Rahami in Elizabeth and Perth Amboy, New Jersey, the decision was made to widely disseminate his photograph early Monday morning on social media — a rarely used tool — and to law enforcement in a “BOLO,” which stands for “be on the lookout for.”

According to law enforcement officials, a woman in Linden, New Jersey, had seen the photo of Rahami and thought he resembled a man sleeping in the foyer of Merdie’s Tavern on East Elizabeth Avenue. She called police and about 10:30 a.m. a police officer went to rouse the man, later identified as Rahami.

This graphic shows the evolution of the weekend's New York City and New Jersey bombings and the subsequent arrest of Ahmad Khan Rahami.

“He [the officer] went to rouse the person, who was sitting in the doorway,” Linden police Capt. James Sarnicki said Monday in an interview. “When the person lifted up his head, he saw that the person had a beard and resembled the individual that’s on the wanted poster that was circulated by the FBI and other law enforcement agencies.”

The officer then said “show me your hands,” at which point Rahami went to his side, pulled out a handgun and fired a round at the officer, striking him in the abdomen area, Sarnicki said. The officer was struck in his protective vest and didn’t suffer serious injuries. More police arrived and a short gunfight followed in which Rahami was wounded in the arm and leg and taken into custody, Sarnicki said. None of the police officers was identified.

NYPD investigators tried to use facial recognition software to identify Rahami, but the images of him captured on surveillance cameras around the bombing sites were either too grainy or didn’t show his face at the proper angle to compare his image to databases, the law enforcement official said.

NYPD spokesman Stephen Davis said the task force was able to develop leads quickly because of the way the crime scenes were preserved.

Special agent Michael Whitaker of the FBI office in Newark, which is part of the task force, said the Rahami case shows how important social media have become as a law enforcement tool, particularly when it allows the public to provide leads.

For O’Neill, who was formally sworn in as the 43rd police commissioner Monday by Mayor Bill de Blasio, the bombing case was his baptism by fire and also reaffirmed the importance of social media.

advertisement | advertise on newsday

“If we can get everybody in the city engaged in helping us and keeping safe, that is the way to go, this is the wave of the future,” O’Neill said.

With Chau Lam and Tom Brune