A customer pulled a gun and shot a man who police say had jumped over the counter of a Phoenix pharmacy and pointed a gun at people while demanding opioids.

Authorities didn't fault the customer, saying he was in the right place at the right time to stop the armed robbery attempt Tuesday night, police Sgt. Jonathan Howard said.

The suspect has been identified as 30-year-old Stephen Holguin, but police didn't immediately release any other information on him.

Howard said the customer has been interviewed and released pending further investigation, but has declined to be identified or participate in media interviews at this time.

Arizona is among a number of states grappling with a surge of opioid abuse. The state had 790 opioid deaths in 2016, a 16 percent increase from 2015.

The scene in the Walgreens pharmacy was "full of chaos" when officers arrived, Howard said.

Police found a dozen employees and customers hiding from the wounded suspect who had taken refuge in an area at the back of the store.

Officers directed people to leave the business before a police dog was used to pull the wounded suspect away from his gun, Howard said. Officers administered CPR but the man died at the scene.

"At this point, it seems to be a case of a good Samaritan in the right place at the right time," Howard said.

Arizona has a "defense of third person" law that allows someone to use deadly force against another person who is threatening or injuring a third person.

Under the law, a person who uses deadly force is justified in doing so if they perceive that lives are in danger.

Customer Janice Gordon-Caddy said she arrived at the store as the incident was unfolding.

"I saw the workers from the pharmacy running out with their hands raised in the air. They were crying and screaming," she told The Arizona Republic .

"Next thing I know, the SWAT team and all the police officers and everybody are going in with their guns; it was just a madhouse," she added. "That probably would have been much worse if there hadn't been a customer in there armed."

Phil Caruso, a spokesman for Walgreens' headquarters in Chicago, said officials were working with Phoenix police about any store surveillance video that showed the incident.