By Ros Krasny

BOSTON (Reuters) - Republican Scott Brown won a bitter Senate race in Massachusetts on Tuesday, dealing a sharp blow to President Barack Obama's legislative agenda and casting doubt on the fate of his sweeping healthcare overhaul.

Brown's defeat of Democratic state Attorney General Martha Coakley robbed Democrats of the crucial 60th Senate vote they need to overcome Republican procedural hurdles and sent shudders of fear through Democrats facing tough races in November's congressional elections.

What once seemed to be an easy Democratic victory turned into a desperate scramble in the last few weeks as Brown surged ahead on voter fears over the economy, the healthcare reform bill and Obama's agenda.

Brown was leading 52 percent to 47 percent with 90 percent of the precincts counted.

The come-from-behind victor had promised to be the 41st Republican vote against the healthcare overhaul in the 100-member Senate.

But more broadly, the stunning upset in heavily Democratic Massachusetts raised the specter of large losses for Democrats in congressional elections in November and left Democrats scrambling to provide explanations.

Obama, who won almost 62 percent of the state's vote in the 2008 presidential election, made a last-minute appeal in Massachusetts on Sunday to try to ignite enthusiasm for Coakley's campaign to replace the late Senator Edward Kennedy, a Democratic icon and longtime champion of healthcare reform.

In Washington, Obama spokesman Robert Gibbs said the president did not believe healthcare reform would fail if Coakley loses. Democratic leaders vowed to push healthcare reform through Congress whatever the results in Massachusetts.

HEALTH STOCKS MOVE HIGHER

Expectations the Brown victory could be the death knell for healthcare reform drove health insurance and drug company stocks higher on Tuesday, lifting the Dow and the S&P 500 to 15-month closing highs.

Election officials reported heavy turnout as voters braved drizzle and light snow to get to the ballot box. Local media reported long lines at some polling places.

Millions of dollars had flooded into the state to buy nonstop television advertising for both sides, transforming a relatively sleepy contest into a bitter brawl.

Coakley was criticized for running a lackluster campaign, taking almost a week off from the campaign trail around Christmas as Brown's appeal was on the rise.

Massachusetts last elected a Republican to the Senate in 1972, but the shift could not have come at a worse time for Obama. Democrats control 60 votes in the Senate to 40 for the Republicans, and the loss of one Democrat could doom the healthcare bill.

(Writing by John Whitesides; Editing by Peter Cooney)