At first, the partying unfolded peacefully Wednesday night with fans gathering in the streets and uncorking champagne, dancing and hugging strangers after their team won its third championship in five years with a 3-2 victory over Kansas City in the winner-take-all seventh game.
"I knew they were going to win. It's the Giants. They do this all the time," San Francisco native Barbra Norris, 54, said of the team's odds-defying victory in Kansas City the night after a 10-0 loss.
But in some areas, the atmosphere grew rowdier as the night wore on. Fans climbed onto buses and sprayed graffiti on the windows of businesses. Nine police cars were damaged, Police Chief Greg Suhr said Thursday.
"I was very disappointed in the people who took away from the celebration," he said.
Most of the 40 arrests were for public drunkenness, Suhr said. He said three officers were treated at the hospital for injuries they sustained when they were hit by bottles.
Violence left four people injured in separate incidents, but it was not yet known if the shootings and stabbings were linked to World Series revelry, police said.
Authorities said a 54-year-old man and a 19-year-old woman were stabbed in separate incidents in the city's Mission district late Wednesday and early Thursday, respectively. Both were expected to live.
A 19-year-old man was shot in the arm in the same neighbourhood Wednesday, and another person walked into a hospital with a non-life-threatening gunshot wound, police said. No arrests had been made yet.
Shortly after the celebrating began, Officer Gordon Shyy, police spokesman, said officers made "a handful of arrests" as fans filled the streets and blocked traffic around the Civic Center, in the Mission District and on Market Street within walking distance of AT&T Park.
Bottles were hurled at police as they attempted to disperse crowds and help firefighters put out bonfires, Shyy said.
The San Francisco Chronicle reported that the region around Third and King streets was especially raucous with thousands of fans spraying beer, smashing bottles, lighting fires and setting off fireworks. Police in riot gear lined up three rows deep, and revelers began tossing bottles at them, the newspaper reported.
The wild street scenes lasted into the early hours Thursday. Shyy said crowds were still in the streets in some areas and stoking bonfires shortly after midnight. But by early Thursday morning, he said crowds had dispersed.
Earlier in the night, across from San Francisco City Hall, where the exterior lights had been glowing Giants orange all week, more than 9,000 people gathered in an outdoor plaza. The city set up a Jumbotron for viewing the game and a vendor sold hot dogs — but no beer.
"You come out here to feel the pulse of the city. When it's the seventh game, you want to get the vibe," said Geoff Goselin, 61.
The crowd sang "Let's Go, Giants" whenever their counterparts 1,800 miles away rooted for the home team and chanted a prophetic "M-V-P" whenever Giants ace Madison Bumgarner took the mound.
"Bumgarner is the beast, the man," Aden Bacus, 41, shouted after the exhausted pitcher secured the final out with the potential tying run on third base. "I'd be lying if I told you I wasn't nervous there at the end."
Amid the revelry, Mayor Ed Lee said the city would host a parade and celebration for the team Friday.
Seavey reported from Phoenix.