BOSTON — State Rep. Martin Walsh was elected mayor of Boston on Tuesday, defeating City Councilor and fellow Democrat John Connolly in the hard-fought race to succeed longtime Mayor Thomas Menino.

With 95 percent of the votes counted, unofficial totals showed Walsh with 51 percent to 49 percent for Connolly, who conceded less than two hours after the polls closed.

“We came up short tonight, but I am very proud of how we ran this campaign,” Connolly said. “This campaign looked like the entire city of Boston from Day 1 and I’m so proud of that.”

Turnout was brisk as voters cast ballots in a mayoral election that for the first time in two decades didn’t include Menino’s name at the top of the ticket.

Walsh, 46, had relied on support from labor organizations to help his get-out-the-vote drive. A union official before being elected to the Massachusetts House in 1997, Walsh has remained active in union affairs as a lawmaker.

Walsh said he was campaigning right up until the last moment and it was strange not seeing Menino’s name on the ballot.

Connolly made education his core issue and was hoping an “army of moms” that appeared with him at campaign events would help propel him into the top office in New England’s largest city. The 40-year-old father of three was the only candidate to enter the race before Menino announced he wouldn’t run again.

Early in the day, Connolly expressed confidence in his campaign and said he hoped people would respond to putting schools first with the result being “safe streets and a great economy.”

While both Connolly and Walsh raised similar amounts of money — just over $1.8 million for Walsh and nearly $1.9 million for Connolly as of Oct. 15 — Walsh benefited from outside spending on television ads.

Menino, the city’s longest-serving mayor, announced this year he would not seek another term after more than two decades in office. He has battled a series of health problems in recent years.

Menino did not take sides in the race to succeed him, and on Tuesday would not say for whom he had voted. He added that he had no regrets about his decision to retire.

“I made the decision in March and it’s done. I’m at peace of mind with myself,” the mayor said after voting in his home neighborhood of Hyde Park.

Casino supporters in the state were dealt twin setbacks Tuesday, with voters in East Boston rejecting a proposal by the Suffolk Downs racetrack, and voters in Palmer narrowly defeating a bid by Mohegan Sun to build a resort casino in the western Massachusetts town.

The results promise to dramatically alter the casino landscape in the state, where a 2011 gambling law allows for up to three regional resort casinos.

Voters in Revere approved the Suffolk Downs casino in a separate referendum Tuesday, but favorable votes were needed in both East Boston and Revere before the track could formally apply to the Massachusetts Gaming Commission for a license.