Security experts who track gunfights in Rio de Janeiro have documented dozens of such episodes in favelas like Alemão since the Olympics started last week, raising questions about the huge security operation for the Games. In one episode on Wednesday, soldiers from the federal security force deployed in Rio for the Olympics came under fire in the Vila do João favela. At least two were wounded, including one who was shot in the head.

Before the Games, Mario Andrada, the spokesman for the Rio Olympics organizing committee, had boasted that Rio would be “the safest city in the world” at this time.

On Wednesday, after the latest violence, he defended those remarks.

“An athlete doesn’t regret saying he’ll win before a game,” Mr. Andrada told reporters.

In 2009, when Rio de Janeiro won its bid to host the Olympics, the authorities envisioned their self-described “pacification” of Alemão and other favelas as a crucial factor in their plan to resurrect Rio’s fortunes. Soldiers in tanks rolled into Alemão in 2010, accompanied by police officers who began building a network of outposts.

For a while, it seemed to work.

As the violence subsided, the authorities constructed a stunning aerial tramway network, connecting Alemão’s densely populated hillsides. Directors scouted filming locations in Alemão for scenes in soap operas. A new pub that served craft beers lured outsiders curious for a glimpse into an area that had long been viewed as off-limits.

But by 2014, the gangs were aggressively clawing back at the police. One of these is the Red Command, which traces its origins to the 1970s, when imprisoned leftist militants banded together with common criminals. The gang built on longstanding ties with Colombian cocaine suppliers to exert considerable sway across Alemão and other areas of Rio de Janeiro.