Robert L. Ehrlich Jr. scored a surprisingly decisive victory over Democrat Kathleen Kennedy Townsend yesterday, becoming the first Republican to claim the Maryland governor's mansion in more than 30 years and apparently generating enough GOP energy to knock off the powerful Democratic speaker of the House.

Townsend, the state's lieutenant governor, won strong support in the Washington suburbs and in Baltimore, a formula that worked for her and Gov. Parris N. Glendening in the past two elections. But Ehrlich, a four-term congressman from suburban Baltimore, was boosted by huge leads and enthusiastic turnout in his home base, in Western Maryland and on the Eastern Shore.

"Welcome to history," Ehrlich told a boisterous crowd gathered at a Baltimore hotel. "This is an incredible night."

Townsend conceded defeat shortly before 11:30 p.m., looking remarkably cheerful and relaxed. She offered Ehrlich her congratulations and her help in resolving a growing state budget crisis that threatens to cripple his first year in office.

"I know we came up short, but we stood up for our beliefs," Townsend told supporters. "He was a formidable opponent. He ran an effective campaign."

Townsend won the support of nine in 10 black voters, despite a concerted effort by Ehrlich to tap into the loyal Democratic base. But she faltered with white voters by an unexpectedly large margin, particularly white women, nearly 60 percent of whom voted for Ehrlich, according to exit polls.

Townsend's poor showing derails her roller-coaster political career, which once prompted speculation that she would be the first woman elected to the White House.

She was originally expected to coast to victory in the heavily Democratic state, but her gubernatorial bid foundered under the direction of an inexperienced campaign team and the burden of the public's growing dislike for Glendening, her political partner.

Glendening, for his part, blamed Townsend, saying she conducted "one of the worst-run campaigns in the country."

"She was a great lieutenant governor, and she would have been a great governor with all kinds of options available," Glendening said. But "she had a very small group of advisers, and they put on the oddest campaign for governor anybody has ever seen. You have to remember your base, and they did not."

Ehrlich becomes the first Republican to be elected governor of Maryland since Spiro T. Agnew defeated a segregationist Democrat in 1966. Ehrlich's running mate, Michael S. Steele, becomes the first African American elected to statewide office in Maryland history.

Ehrlich's victory increases the likelihood of a substantial expansion of legal gambling in the state and will trigger a massive upheaval in state agencies and county courthouses and on local boards and commissions, whose officials are appointed by Maryland's powerful chief executive.

In the race for Ehrlich's congressional district, Democrat C.A. Dutch Ruppersberger bucked the Republican trend, defeating Helen Delich Bentley. Democrat Christopher Van Hollen Jr. also won a closely fought House race in the Washington suburbs, unseating Constance A. Morella (R) and fulfilling Democrats' hopes of increasing their presence in the state's congressional delegation. Democrats now hold six of the eight seats.

But Ehrlich's unexpectedly strong showing helped lift other Republicans to State House offices. Democrats maintained their overwhelming dominance in the General Assembly but lost several key seats to Republican challengers, apparently including that of House Speaker Casper R. Taylor Jr. (D-Allegany).

Aides said Taylor had not conceded defeat, but his opponent, LeRoy E. Meyers Jr., appeared to score a stunning upset after running against Taylor's support for gun-control measures. The local businessman, a political unknown, also benefited from radio ads Ehrlich broadcast in the conservative Western Maryland district, urging residents to support a slate of GOP candidates to help him govern in Annapolis.

The governor's race was by far the most expensive in state history, nearly doubling the $12 million spent in 1998. Ehrlich spent close to $11 million; Townsend spent more than $9 million, according to campaign officials. The money paid for a relentless fusillade of mostly negative television ads.

Ehrlich and Townsend agreed on many major issues, from building a controversial new highway known as the intercounty connector across Washington's northern suburbs to fully funding a historic expansion of state spending for public schools. They both pledged to avoid raising taxes, despite the biggest projected budget shortfall in state history.

So instead of debating issues, each fought to shape the other's image.

Townsend tried to paint Ehrlich as a right-wing zealot who would roll back Maryland's tough gun restrictions and environmental regulations and its pioneering laws that seek to reserve many state contracts for minority-owned businesses.

Ehrlich blasted Townsend as an inexperienced politician who mismanaged anti-crime programs under her control and helped Glendening dramatically increase state spending.

Glendening's popularity has plummeted in recent months, as a result of his recent divorce and subsequent remarriage to his former deputy chief of staff, and as a result of the state's darkening fiscal outlook.

Pre-election polls showed that Townsend's partnership with Glendening was enormously damaging to her among independent voters and even some Democrats. Yesterday, many voters said Ehrlich's criticism of the "Glendening-Townsend administration" had hit home.

Barbara Singer, 60, a self-employed bookkeeper who lives in Baltimore County, voted twice for the Glendening-Townsend ticket. But she voted for Ehrlich yesterday because "we're in a horrible deficit," for which she thinks Glendening is largely to blame.

"I voted more against Kathleen Kennedy Townsend than for Ehrlich," Singer said. "I don't think she's up to the task of being governor."

Larry Derx, 48, a branch manager of a mortgage company who lives in Brookeville, said he, too, swung to Ehrlich, though he is a registered Democrat.

Glendening and Townsend, he said, have "spent a lot of our tax dollars on frivolous things." Townsend "didn't seem to have her own programs. It just seemed to be a continuation of Parris Glendening. She was just a rubber stamp," he said.

Democrats and Republicans alike had girded for a too-close-to-call race, marshaling hundreds of lawyers and accusing the other side of cheating during the final days of the campaign.

The election was originally billed as a coronation for Townsend, as analysts predicted that Maryland would open its arms and readily embrace the eldest child of Robert F. Kennedy, a leader of her generation in the famous family.

But the coronation quickly turned into a horse race as Ehrlich pursued a shrewd strategy of lying low and letting attention focus on Townsend, her campaign's frequent missteps and her ties with Glendening.

Ehrlich acted quickly to neutralize his biggest drawback: his status as a Republican.

He cast himself as a fiscal conservative and a social moderate to make himself palatable to a broad range of voters. And he went on the attack from the start, criticizing Townsend's limited political experience, her privileged upbringing and her acceptance of a "culture of corruption" in Annapolis that Ehrlich said had been bred during 33 years of Democratic rule.

Ehrlich blamed "overspending" by the administration for creating a projected budget shortfall of more than $1.7 billion over two years -- the largest in state history.

To plug the budget hole, Ehrlich proposed to legalize slot machines at horse racing tracks, a politically popular idea that would come nowhere near raising enough money to solve the budget crisis.

Early on, Townsend did little to fight back. With a 15-point lead in the polls, she hired loyalists from her State House office to staff her campaign rather than experienced political operatives. She pursued an above-the-fray strategy, generally refusing even to mention Ehrlich's name.

Her central theme was the lofty but confusing notion that every person has an "indispensable destiny." And she attempted to run as a centrist -- a "governor for all of Maryland," as she put it -- offering a 32-page blueprint filled with earnest proposals for incremental improvements in dozens of state programs, but no broad initiatives.

The coup de grace of the early Townsend campaign was her choice of running mate: Charles R. Larson, a lifelong Republican who had commanded U.S. forces in the Pacific and who switched parties just to join Townsend's ticket.

Democrats initially hailed the choice as a brilliant move that would play well with white males, among whom Townsend was weak. But the strategy backfired when black leaders in Prince George's County complained that Townsend had passed over qualified black candidates and was taking the African American vote for granted.

Ehrlich, meanwhile, chose Steele, the only black state GOP chairman in the nation, as his running mate. The selection was intended to inoculate the ticket from Democratic claims that a Republican governor would turn back the clock on civil rights. Such attacks sank Ellen R. Sauerbrey, the GOP gubernatorial candidate in 1998.

But as the campaign progressed, Steele's selection allowed Ehrlich to make a serious play for the state's African American vote -- or at least to argue that black voters should think twice before voting against a man who could be the first black lieutenant governor in Maryland history.

By late summer, Ehrlich had pulled even in the polls, and Townsend was dealing with the fallout from a grand jury investigation into a crime control agency she ran.

As senior Democrats clamored for Townsend to revamp her campaign, she hired two veteran operatives who had steered Glendening to victory in 1998, dropped the "indispensable destiny" line and began courting African Americans, labor and liberal interest groups as ardently as any machine Democrat.

She also went on the attack. In television ads, on the campaign trail and, most famously, in the campaign's single televised debate, Townsend tried to paint Ehrlich as a right-wing "extremist" who opposes abortion rights, civil rights, labor and affirmative action and has voted to defile the Chesapeake Bay.

Townsend blasted Ehrlich most vociferously on the issue of guns, criticizing his vote to repeal a federal ban on semiautomatic "assault weapons" and against a state ban on cheap handguns known as Saturday night specials. One Townsend television ad branded Ehrlich as "the NRA candidate for governor."

Kendel Ehrlich looks on as her husband, Gov.-elect Robert L. Ehrlich Jr., talks to supporters in Baltimore. "Welcome to history," he told them.