SAN FRANCISCO — The crowd gathered to watch the San Francisco Giants in Game 7 of the World Series went silent at the end of the ninth inning as the ball popped foul by Kansas City Royals’ catcher Salvador Perez hung in the air above Giants third baseman Pablo Sandoval. Then cheers erupted, sounding off across the city when the ball smacked inside Sandoval’s glove to clinch a narrow 3-2 defeat.

It was a hard-fought victory that didn’t come any more easily for the fans than it did for the team. But celebrations that started with that eruption of joy continued well into the night and turned more rowdy as the night wore on.

Police officers — several in riot gear — kept a watchful eye as thousands paraded down Market and Mission streets in San Francisco. Some fans set fires or jumped on top of Muni buses, prompting the agency to shut down service on city streets. Other fans spray-painted walls, and hundreds threw water bottles at officers and yelled out threats as the officers tried to disperse crowds. Police swarmed in to move crowds along from one street to another until they finally broke up about 12:30 a.m.

There was no official information on arrests or injuries but police said two men were shot during the celebrations. The wounds were not considered life-threatening.

The celebrations started out fairly tame for fans who were still reeling from the close win. Thousands had gathered to watch the game at the city’s Civic Center, carrying flags in a sea of orange and black at San Francisco’s biggest baseball party.

City Hall was a lit in a striking orange.

“I was so stressed out knowing that last out was still there,” said San Francisco resident Diana Cholden. “We brought it home, but they know how to torture us.”

San Francisco native Valerie Blueford, a 36-year fan, held a sign over her head that said, “Yes, Yes, Yes” as she jumped up and down after the final out.

“I grew up at Candlestick Park, and now I get to see (the Giants) win three World Series championships in my lifetime,” she said as she beamed. “It means a lot to me.”

Wednesday’s atmosphere of celebration was a stark contrast to the night before, when silence fell over the outdoor gathering spot as the Royals coasted to a 10-0 victory.

On Wednesday, fans showed no signs of leaving or quieting as it came down to the wire, and after the win was secured, police kept a steady watch on revelers.

Within minutes of the game ending, a crowd blocked King Street outside the stadium, chanting “Dynasty! Dynasty!” as smoke from fireworks lingered overhead. Scores of police, some in plain clothes, began separating crowds as thousands poured from restaurants, bars and parties to celebrate the Giants’ third World Series win in five seasons.

Bonfires, fueled by whatever people could find along the roadway, including newspaper racks, cans and bottles, popped up in the middle of streets along Mission Street, with crowds dancing around them. People also climbed on top of street lights and buses, or jumped in an out of cars along Market Street.

Some set off fireworks dangerously close to crowds near AT&T Park.

The celebratory energy was abundant long before the collective sigh of relief upon Sandoval’s catch to end the Series.

In the third inning, with the game tied 2-2, Mission District resident Luis Portillo fed off the excitement of the crowd.

“We still have that hope,” the 20-year-old said. “Whatever happens, happens.”

“These are true fans,” said Brickhouse Cafe & Saloon doorman Will Lynch. “They come here from different parts of city to be near the ballpark.”

Lynch said he expected celebrations to be just as raucous as those that came after the team’s 2010 and 2012 World Series wins. But he thought it would be more “controlled” this time around.

“I think some fans were embarrassed by things in the past,” Lynch said of the burning of a city bus in 2012.

Tenderloin resident Stu Barnes, 33, said the coming together of people from all over the Bay Area was reflective of the City by the Bay being a “baseball city.”

“Take a look around you,” he said while watching the game in black-and-orange pinstripe pants from Hemlock Tavern on Polk Street. “There are weirdos in here that can tell you the E.R.A. of a bullpen pitcher. It’s beautiful.”

Giants fan Marge Rothon summed up her feelings on Game 7 as she sat on a sidewalk outside a south of Market bar as the bottom of the sixth inning ended.

“It’s out counting time,” she said.

Follow David DeBolt at Twitter.com/daviddebolt. Follow Katie Nelson at Twitter.com/katienelson210. Follow Thomas Peele at Twitter.com/thomas_peele.