It was widely assumed that San Francisco Supervisor Scott Wiener had a big lead over fellow Supervisor Jane Kim in their race for state Senate — until the votes came in on election day and that perceived edge evaporated. He still won the primary, but not by the margin many expected.

After the votes were counted Tuesday night — and there are still thousands of vote-by-mail and provisional ballots to count — Wiener led Kim by just two percentage points.

Kim came into the race for termed-out Democratic state Sen. Mark Leno’s seat with fewer endorsements and less money. Political analysts attribute her rise at the polls Tuesday to a “Bernie bump.” After Bernie Sanders endorsed her, fresh money rolled in and Kim went on a campaign blitzkrieg of appearances with the presidential candidate, who had cameras following him everywhere.

She actually did better in San Francisco than the Vermont senator — winning 78,417 votes to Sanders’ 69,688, at last count. Because more renters and younger, progressive voters — who are more likely to support Kim because of her progressive credentials — tend to show up for presidential elections, the gap between the two Senate candidates will likely remain tight heading into November. But can Kim win the state Senate seat this fall without the attention Sanders provided? Wiener said no.

“She was basically his running mate in the weeks leading up to the election,” Wiener said. “She took advantage of a great campaigning opportunity, but nevertheless, I came in first place. We knew the race was going to tighten, but the fact that we were able to withstand that tidal wave is a testament to the work I have been able to do on good policy.”

But others aren’t so sure. The Sanders spotlight helped, said political consultant Jim Ross, but what Kim did with it mattered more. She used the platform to spread her message: as a fighter for the middle class, for affordable housing, for free City College, he said, rattling off Kim’s campaign themes.

“She has a more compelling message that was delivered more effectively,” said Ross. “She found a real way to differentiate herself. It’s not so much that Sanders drove her success, but that he helped crystallize in people’s minds the difference between who Jane Kim and Scott Wiener are. Candidate campaigns come down to creating a contrast, and she did that effectively.”

Kim also out-campaigned Wiener in the 60 days leading up to the primary, said political consultant John Whitehurst, who worked on Leno’s 2008 state Senate campaign. As voters’ mailboxes became clogged with glossy mailers, Kim bought TV commercials. Viewers saw her in two attention-getting ads as “black-belt Jane” and “fighter Jane” who stands up for regular San Franciscans, he said.

Kim also secured the last-minute endorsement of state Democratic Party Chairman John Burton, who called her “the real Democrat.” That set off allegations of foul play because Wiener had won the endorsement in February of the state Democratic Party itself. It also gained Kim more visibility.

Wiener said he spent his time extolling his success on core issues like expanding public transportation and protecting the environment. That’s what won him the primary and will help him win in November, he said.

He also had a TV commercial and mailers, and he canvassed neighborhoods. But he didn’t have someone like Sanders.

“The margin was surprisingly close, particularly given that Hillary Clinton beat Bernie Sanders soundly inside of San Francisco,” Whitehurst said. “Kim exceeded expectations, but she also had a very strong campaign that created several things that were out of the box. There’s the Sanders endorsement, the videos, the Burton letter — all things that enabled voters to see her for the first time. Scott was a lot more well-known in the beginning of the race, but she has had more exposure recently.”

Whoever wins the District 11 state Senate seat, representing San Francisco and northern San Mateo County, could hold the position for as many as 12 years. And the race will probably be neck and neck the entire way, said Eric Jaye, Kim’s campaign consultant.

“It’s always difficult when you start behind and are being dramatically outspent,” Jaye said. “Jane has a strong message. It’s about making the city and state more affordable for middle- and working-class families. Wiener was not able to offer a message that could compete with that. It’s going to be a tight race.”

Kim said she had hoped to come within five percentage points of Wiener in the primary. At her election night party in SoMa, she took the stage with 30 of her biggest backers, including Chinatown power broker Rose Pak, as the first numbers came in. Music throbbed and people cheered. Later, she said it was overwhelming and surprising.

“This is my fifth race for office, and I have always won on the ground,” Kim said. “In a campaign where we were outspent, it’s exciting to see us do so well. The campaign and race really continue onward from here on out until November.”

Lizzie Johnson is a San Francisco Chronicle staff writer. Email: ljohnson@sfchronicle.com Twitter: LizzieJohnsonnn