It was a hectic weekend, and the culmination of six years of work by Mr. Bloomberg, who founded his national coalition, Mayors Against Illegal Guns, in 2006, galvanized by a series of grisly police shootings in New York.

In 2005, weeks after being elected to a second term, the mayor found himself waiting for hours in an emergency room as doctors tried in vain to save the life of a police officer, Dillon Stewart, who had been shot in a car chase in Brooklyn.

Officer Stewart had been wearing a gun-resistant vest, but the bullet pierced his skin a few millimeters from the armor. At the hospital, detectives examined the vest in front of the mayor; when they pressed the vest with their hands, Mr. Bloomberg could see blood rise to the surface.

The mayor was angry that New York’s gun laws, among the strictest in the country, did little to protect against the use of guns bought illegally in other states. In his inaugural speech on New Year’s Day 2006, he pledged to curb “these instruments of death,” although, at the time, the gun-control movement was at a standstill.

“Virtually nothing was being said in Congress by some of the strongest allies; they basically had walked away from the issue,” said Robert Walker, who served as president of the Center to Prevent Handgun Violence, the forerunner to the Brady Center, in the late 1990s. Mr. Bloomberg, he said, “filled the void at a time when it really needed to be filled.”

Friends of Mr. Bloomberg said he came to view guns, like tobacco and unhealthy food, as a dangerous consumer product, and he could not fathom why lawmakers did not take steps to curtail their use. There was also a quixotic element to taking on the gun lobby that appealed to the mayor, who relishes challenges that others view as insurmountable.

“It’s a little bit of a knight-in-shining-armor mentality,” said Paul Helmke, who stepped down last year as head of the Brady Center. “When he sees a group that is perceived as powerful, like the N.R.A., it’s like the challenge is there to take them down.”