Get breaking news alerts and special reports. The news and stories that matter, delivered weekday mornings.

The desperate search for a 28-year-old man wanted in connection with a series of blasts that terrorized New York and New Jersey over the last three days ended Monday in a gun battle with police officers.

Ahmad Khan Rahami was charged Monday evening with charged with five counts of attempted murder of a law enforcement officer, according to the Union County, N.J., prosecutor.

The prosecutor's office also charged him with second-degree counts of unlawful possession of a weapon and possession of a weapon for an unlawful purpose. A state judge set bail at $5.2 million.

Ahmad Rahami in a police mugshot. Union County, N.J., Prosecutor's Office

Rahami, who could face up to 20 years for each count of attempted murder of a law enforcement officer, was taken into custody after he was shot in the leg during the 10:30 a.m. confrontation in Linden, N.J., law enforcement sources said.

Rahami managed to shoot one police officer in the hand and another in the bulletproof vest before he was apprehended, they said.

The injuries sustained by Rahami, who was being treated at University Hospital in Newark, and by the officers did not appear to be life-threatening, officials said.

"When I was at the scene, initially, he was conscious and awake," Linden police Capt. James Sarnicki said of the suspect.

Key developments

Authorities say flip phones used on explosive devices in Seaside, N.J., and the Chelsea section of Manhattan were bought at same store last year.

Unexploded devices were also discovered blocks from the Chelsea blast and near an Elizabeth, N.J., train station.

FBI agents in Brooklyn stopped "a vehicle of interest" in the investigation of the Manhattan explosion. Five occupants were detained for questioning.

The suspect traveled to Pakistan and Afghanistan within the last 10 years.

Rahami's father, Mohammad Rahami, told NBC News he had "no idea" of what his son was planning.

The suspect was discovered sleeping in the doorway of a bar.

Congressman says Rahami sought help getting visa for pregnant Pakistani wife.

FBI and police at the scene of Ahmad Rahami's capture Monday in Linden, N.J. Ed Murray / NJ Advance Media for NJ.com

Rahami was captured after police got a call about a man sleeping in the doorway of a local bar, Sarnicki said.

When police arrived, one of the officers "tried to rouse him," Sarnicki said. "The gentleman on the ground picked up his head, and the officer saw that he had a beard and resembled the wanted person from the poster ... from the bombings."

After the officer ordered the suspect to show his hands, the man "pulled out a handgun and fired one shot at the officer, striking him in the abdomen," Sarnicki said. "Fortunately, the officer had a bulletproof vest on."

The Linden cops returned fire, hitting Rahami several times, Sarnicki said.

A local business owner told NBC News that he heard what he thought at first were fireworks.

Let our news meet your inbox. The news and stories that matters, delivered weekday mornings. This site is protected by recaptcha

"But then we took a peek, and there were cops firing, and the guy went down in front of the building," said the owner, who declined to give his name.

Meanwhile, President Barack Obama reassured a nervous nation that law enforcement was on the case.

"Moments like this, I think, it's important to remember what terrorists and violent extremists are trying to do," Obama said. "They are trying to hurt innocent people and create fear in all of us, to disrupt the way we live."

Obama praised New Yorkers and New Jersey residents after their states were once again targeted.

"Folks around here, they don't get scared," he said. "They're tough. They're resilient. They go about their business every single day."

Later, Obama told reporters that he had spoken with — and thanked — the wounded Linden officers.

"They are going to be fine, sustained modest injuries, in good spirits," he said. "Just one more reminder of the skill and sacrifice of law enforcement officers."

Rahami was born in Afghanistan, and his family was granted asylum in the United States in 2011, a senior U.S. intelligence official told NBC News. His father, Mohammad, said he had "no idea" that his son was allegedly plotting a bombing campaign.

"I'm not sure what's going on. I'm not sure what's happening exactly," Mohammad Rahami said as he returned to the family's store in Elizabeth, N.J.

Rep. Albio Sires, D-N.J., said Monday on MSNBC's "Meet the Press Daily" that Rahami got in contact with his office in 2014 asking for help because his pregnant wife's Pakistani passport had expired.

"I assumed she did" get the visa, Sires said — before adding a personal assessment of Rahami: "He was kind of nasty, too."

Rahami is the man seen in surveillance footage taken Saturday night in Manhattan's Chelsea neighborhood, the site of an explosion that injured 29 people, a senior law enforcement official with direct knowledge of the investigation told NBC News.

The blast was so powerful that it sent a dumpster flying more than 150 feet down the sidewalk and shattered windows more than a block away. Police later found a second bomb that did not detonate a few blocks away on 27th Street.

The FBI said it does not yet know why the Chelsea neighborhood was targeted.

Other physical evidence links Rahami to the pressure cooker bombs that went off or were found in New York and New Jersey, a senior law enforcement official said.

Rahami, who is a U.S. citizen, was identified as a suspect after a fingerprint was found on one of the devices that failed to detonate, a senior law enforcement official told NBC News.

Investigators also found more information that pointed to Rahami on cellphones that were wired to the unexploded bombs, the official said.

"He certainly seemed to do virtually nothing to cover his tracks," the official said.

New Jersey resident Ahmad Khan Rahami, 28, is a suspect in multiple bombing incidents in New Jersey and New York City. New Jersey State Police / EPA

But the official downplayed any talk of Rahami's being part of a "cell" and said that at this point investigators have no idea whether anyone else was involved. There was no indication that an ISIS cell was operating in New York City, FBI Assistant Director-in-Charge William Sweeney said at an afternoon news conference.

New York Mayor Bill de Blasio said at the same news conference: "There is no other individual we are looking for at this time."

Nonetheless, New York Gov. Andrew Cuomo said officials could not rule out international terrorism.

"Today's information suggests it may be foreign related, but we'll see where it goes," Cuomo said Monday on MSNBC's "Morning Joe."

Rahami visited Afghanistan as recently as April 2013, a senior U.S. official told NBC News on Monday evening. In the last decade, Rahami has also made several trips to neighboring Pakistan — and his older brother Mohammad is believed to still be in Afghanistan, the official said.

Rahami was caught hours after a backpack that appeared to contain pipe bombs exploded in Elizabeth, N.J., as a police robot examined it near a New Jersey train station. That blast, which happened shortly before 1 a.m. ET Monday, was the second in New Jersey since Saturday morning.

Elizabeth Mayor Chris Bollwage told reporters that the robot was "cutting into the device when it exploded."

Multiple senior law enforcement officials told NBC News that the device in Elizabeth appeared similar to the one that exploded Saturday morning in Seaside Park, N.J. The package, a backpack, was found by two men in a garbage can about 300 feet from the front door of a crowded pub in Elizabeth, Bollwage said.

When they saw wires and pipes, they dropped it and immediately went to police headquarters, he said.

"We do not believe those two are involved," the mayor said. "We believe they did the right thing."

Five hours later, FBI agents were spotted at the Rahami family's fried chicken restaurant in Elizabeth.

While investigators prepared to question Rahami, the FBI was still grilling five people who were taken into custody after authorities stopped a "vehicle of interest" in the New York blast at about 8:45 p.m. ET Sunday near the Verrazano-Narrows Bridge in New York City.

The five people were later released without charges, authorities said.

There was a suspicious package with multiple improvised explosive devices this evening at the Elizabeth Train Station in NJ. #Elizabeth — FBI Newark (@FBINewark) September 19, 2016

Bomb techs from the FBI, Union County, & the New Jersey State Police have arrived on the scene and are now rendering the area safe — FBI Newark (@FBINewark) September 19, 2016

Investigators analyzing possible similarities between the two devices found in Manhattan and the one that detonated in Seaside Park noted that all three apparently contained old-style mobile flip phones, according to officials familiar with the probe.

According to federal investigators, one of the factors linking the New York and New Jersey bombs was the fact that the flip phone from the Seaside Park device, which was bought in February 2015, and the phone attached to the pressure cooker bomb on 27th Street, which was bought in May 2015 — were both purchased at the same Family Dollar store in Perth Amboy, N.J.