Markey, 66, won the early backing of Kerry and much of the state's Democratic political establishment, which was set on avoiding a repeat of the stunning loss it suffered three years ago, when Republican state Sen. Scott Brown upset Democratic state Attorney General Martha Coakley in the election to replace the late Democratic Sen. Edward Kennedy.
Gomez, a 47-year-old businessman and former Navy SEAL, positioned himself as a moderate and Washington outsider who would challenge partisan gridlock, contrasting himself with Markey, who was first elected to the U.S. House in 1976.
Markey had an advantage of about 8 percentage points over Gomez with most precincts reporting late Tuesday, according to unofficial returns. He took to Twitter to thank voters after his victory.
"Thank you Massachusetts!" he tweeted. "I am deeply honored for the opportunity to serve you in the United States Senate."
Markey outspent Gomez throughout the race, and Republicans were unable to match a well-oiled Democratic field organization in an election that saw relatively light turnout in much of the heavily Democratic state.
Kerry left the Senate this year after being confirmed as U.S. secretary of state. Markey will fill out the remainder of Kerry's term, which expires in January 2015, meaning that another Senate election will be held a year from November.
Though Markey has a lengthy career in Congress, he will become the state's junior senator to Elizabeth Warren, who has been in office less than six months after defeating Brown in November.
Markey led in pre-election polls but said Tuesday when he voted with his wife in his hometown, Malden, that there was no overconfidence in his organization. He had said the campaign called or rang the doorbells of 3 million prospective voters in the past several days.
"I have delivered a message on gun safety, on a woman's right to choose, on creating more jobs, and I think that message has been delivered," Markey said.
President Barack Obama, former President Bill Clinton and Vice President Joe Biden made visits to Massachusetts over the final two weeks of the campaign to shore up support for Markey.
Gomez said while voting Tuesday in Cohasset, where he lives, that the election was about choosing the future over the past and what he called Markey's failure to take on the important issues despite 37 years in office.
"Where I come from, that is mission incomplete," he said.
In Cambridge, Lori Berenson, 51, said she voted for Markey mainly because she was skeptical of one of Gomez's main campaign pitches: his request for just 17 months in office.
"He thinks in 17 months he's going to accomplish what Markey hasn't done in 37 years?" she said.
But David Wanders, 43, of Stoughton, said he voted for Gomez because he felt Markey had been in Washington too long.
"He's a lifer," said Wanders, an independent who voted for Obama in the last election. "I don't think he lives here. He lives in Washington."
Markey spent more than $8.6 million on the race through the end of the last reporting period June 5, compared with $2.3 million by Gomez, according to Federal Election Commission records.
Outside groups also poured about $6 million into the Markey-Gomez contest, in the absence of an agreement between the candidates akin to one that had kept most outside money out of last year's Warren-Brown race.
Among the big independent spenders were a Republican-backed super political action committee funded by John Jordan, a California-based donor, and NextGen, a super PAC financed by another wealthy Californian, Thomas Steyer, who supported Markey largely because of his opposition to the proposed Keystone XL Pipeline, which would carry oil from western Canada to Texas.
Also on the ballot was Richard Heos, affiliated with the Twelve Visions Party.
___
Associated Press writer Steve Peoples contributed to this report.