Just before 9 a.m. on Tuesday, while the cafe at Zabar’s in Manhattan was still busy with the breakfast rush, an unmistakable sound interrupted the chatter: A gun was fired.

The authorities said the shooting was an accident. A man who had just bought a bagel and a cup of coffee was adjusting his waistband, causing the gun he had tucked in his pants to fire, shooting himself in the leg.

About 30 people were inside the cafe, which is part of the upscale market on the Upper West Side, when the gun went off, workers in the store said.

The man, identified by the police as Louis Lester Anderson, 48, hurried away from the store, on the corner of Broadway and 80th Street, and took himself to Mount Sinai St. Luke’s, where he was treated and found by the police. He has been taken into police custody, officials said. The matter was being investigated.