In the process, Miles Scott became a darling of social media and attracted thousands of fans around the country, including the White House.
"When you have an illness, it's very important to know you have a support system," said Gina Futrell, a 51-year-old with multiple sclerosis, who was among a large crowd gathered at Union Square for a chance to so see the "Batkid" in action. "I have an extremely strong support system, and I hope he does too. He's such a little hero."
Batkid was called into service by Police Chief Greg Suhr and spent the day zooming from one "crime scene" to the next. Accompanied by an adult Batman impersonator, Batkid rescued a damsel in distress from cable car tracks, captured the Riddler as he robbed a bank, and saved the San Francisco Giants mascot - Lou Seal - from the Penguin's clutches.
Miles was able to fulfill his wish through the Make-A-Wish Foundation, the city and volunteers who stepped forward to help. He was diagnosed three years ago, underwent chemotherapy treatment and is now in remission.
Batkid had a police escort worthy of a dignitary as he sped around the city in a black Lamborghini with a Batman decal, with officers blocking traffic and riding alongside him on motorcycles. The White House sent out a tweet encouraging Batkid to "Go get 'em!" In a video recording, President Barack Obama said, "Way to go, Miles! Way to save Gotham!"
The crowds grew after each stop, reaching into the thousands by the time Miles got to Union Square for lunch at the Burger Bar atop Macy's. Spectators climbed trees and clambered up lampposts, and police and organizers struggled to keep a path open for the motorcade, which drove past onlookers lining the streets six deep for several blocks.
At Batkid's stop in the city's Russian Hill neighborhood, a woman sat on the cable car tracks in a dress and thigh-high black boots. She had a handkerchief around her mouth, and her hands were bound behind her back.
Batman and Batkid sprang into action, with the aid of a trampoline, as the crowd roared. They rescued the woman and disabled a plastic replica bomb she was tied to.
The two masked superheroes then took off to nab the Riddler as he robbed a downtown bank. They later jetted to the Penguin's kidnapping of Lou Seal.
The 5-year-old at first seemed overwhelmed by the outpouring, quietly working through each scenario with clenched fists and tight lips amid delirious chants of "bat kid, bat kid." But by the time he reached City Hall to receive a key to the city in front of the biggest crowd of the day, Miles was all smiles and bravado.
Though he didn't address the crowd, he raised his fist twice and wore a grin as he was feted with chocolate, an FBI "raid jacket" and a San Francisco Police Department cap. A clothing company donated $10,000 to Miles' family, and San Francisco Mayor Ed Lee proclaimed Nov. 15 to be "Batkid Day Forever."
U.S. Attorney Melinda Haag unveiled an "indictment" charging the Penguin and Riddler with conspiracy as the crowd that stretched for blocks roared with delight.
Miles father, Nick Scott, was asked what the boy liked best about Batman. "The cape, I guess," he said.
The father thanked the crowd, organizers and the city for showing his son a good time.
"This is closure for us," Nick Scott said. "It has been a hard three years."
Miles, who lives in Tulelake in far Northern California, didn't know what was in store for him and thought he was in San Francisco just to get a Batman costume so he could dress like his favorite superhero.
He was diagnosed with leukemia when he was 18 months old and ended treatments in June.
Make-A-Wish has fulfilled similar wishes across the country. In Anaheim, a child became Batman's sidekick, Robin; and in Seattle a child was a secret agent, said Jen Wilson, a spokeswoman for the local organization.
The San Francisco Chronicle, KGO-TV and thousands of volunteers participated in the event. At Union Square, the Chronicle distributed hundreds of copies of special-edition newspapers with the headline "Batkid Saves City."
"This is off-the-hook San Francisco," Suhr said.
Associated Press writers Channing Joseph and Terry Collins contributed to this report.