Republican Charlie Baker has won the governor's race in a nail-biter of an election that seesawed throughout the night and extended into early Wednesday. Democrat Martha Coakley said she wouldn't make any immediate statement and asked her supporters to go home for the night.

Baker will replace Democratic Gov. Deval Patrick, who opted not to seek a third term in office.

About 34,000 votes separated the two, with Baker holding an edge over Coakley with 98 percent of precincts reporting, a margin of victory of about 1.6 percent.

Baker's victory will return the state's top political office to the GOP, and with it, he earns a measure of political redemption. Baker, the former CEO of Harvard Pilgrim Health Care and a top official in the administrations of Republican Govs. William Weld and Paul Cellucci, had lost to Patrick four years ago.

Coakley, who lost to Republican Scott Brown during the 2010 special election to fill the U.S. Senate seat left vacant by the death of Democratic stalwart Edward Kennedy, was also looking for comeback.

This time, she stood to become the first woman elected governor of Massachusetts if elected.

The race was one of the closest in recent memory in Massachusetts and much closer than many observers had anticipated.

Depending on how close the final vote totals are, Coakley could request a recount.

State Secretary William Galvin said if the total margin of victory is one half of 1 percent or less, the losing candidate could request an accelerated recount process. That would require the candidate to collect several thousand signatures.

Galvin, as the state's top elections official, would order the recount.

If the margin is more than one half of 1 percent, the candidate would have to collect 10 signatures in each precinct in the state -- a much higher bar. There is no automatic recount process in Massachusetts.

Baker had worked hard to appeal to the independent and moderate Democratic voters who are key to any statewide race in Massachusetts.

He vowed to hold the lid on taxes, increase the number of charter schools and use tax credits to reward businesses that hire welfare recipients and veterans.

He raised more money than Coakley -- an advantage that was magnified by the more than $8.6 million spent by outside groups supporting his candidacy, nearly all of it from the Republican Governors Association.

Three independents, Evan Falchuk, Scott Lively and Jeff McCormick, were also on Tuesday's ballot. Falchuk got a little more than 3 percent of the vote. Lively and McCormick each got about 1 percent.

In one of the early races of the night, Democratic Sen. Edward Markey won his first full six-year term after prevailing in a special election last year to finish out John Kerry's term. He defeated Republican Brian Herr.

The state's congressional delegation remained all-Democratic.

In a hard-fought race in the state's 6th Congressional District, first-time candidate Democrat Seth Moulton defeated Republican Richard Tisei after upsetting Democratic Rep. John Tierney in the primary.

Moulton, a former Marine, vowed to bring jobs back to the district.

"Now I return to public service, in a different venue but with that same commitment to serve you and to serve our great country," Moulton told supporters.

In the 9th Congressional District, Democratic Rep. William Keating fended off a challenge from Republican John Chapman.

Democratic Rep. Niki Tsongas won re-election in the 3rd Congressional District, defeating Republican Roseann Wofford.

In the race to succeed Coakley as attorney general, Democrat Maura Healey defeated Republican John Miller, becoming the first openly gay attorney general in the country.

Democratic state Auditor Suzanne Bump beat Republican Patricia Saint Aubin and Green-Rainbow member MK Merelice.

In the open contest to replace Democratic state Treasurer Steve Grossman, Democrat Deb Goldberg defeated Republican Mike Heffernan and Green-Rainbow candidate Ian Jackson.

Galvin, a Democrat, fended off a challenge for secretary of state from Republican David D'Arcangelo and Daniel Factor of the Green-Rainbow Party.

As for ballot questions, voters rejected measures that would have repealed the 2011 casino law and expanded the state's bottle-deposit law but backed a question that would allow workers to accrue sick time.

Voters also repealed a law that ties future increases in the state's gasoline tax to inflation.