HARRISBURG — Democratic U.S. Sen. Bob Casey of Pennsylvania withstood a spending spree by coal-industry millionaire Republican Tom Smith to win a second term Tuesday in a race in which the political scion had been heavily favored.

Casey’s victory also cements a seat that Democrats had counted on in the struggle for control of the U.S. Senate.

Casey, 52, of Scranton, an ally of organized labor, had been a heavy, early favorite and was way ahead in polls through August. But Smith, a tea party favorite who made a fortune in western Pennsylvania’s coal-mining industry, invested more than $16 million of his own money in the race, hammering Casey for three months on TV and radio and forcing Casey to mount a strong response in the final weeks.

Although Casey had strong name recognition because he is the son of the late former Gov. Robert P. Casey, many voters were still learning the identities of the candidates in the final days before the election as the men campaigned in the shadow of the presidential race.

Counting spending by outside groups, Smith was on course to outspend Casey, $18 million to less than $9 million after July 1.

Casey first won the office in 2006 after soundly beating arch-conservative Sen. Rick Santorum. Based on preliminary results of an exit poll, independent voters split their votes about evenly between Casey and Smith, representing a huge drop in support for Casey compared with 2006.

The former state treasurer and auditor general ran as a moderate who put the interests of this diverse state ahead of party leaders. Casey had supported President Barack Obama’s signature policies, including his sweeping health care law, economic stimulus bill and overhaul of financial-sector regulations.

He also supported bailouts of banks and the American automakers, borrowing authorizations to avert a national default, tougher pollution standards on coal-fired power plants, higher income taxes on higher earners and an increase in the minimum wage. He helped write bills to allow the federal government to impose sanctions on countries that manipulate their currency to gain trade advantages, to create a one-year tax credit for small businesses that add employees, and to impose stronger federal regulation on the gas drilling technique known as hydraulic fracturing.

Casey had attacked Smith as too extreme for Pennsylvania, and won nearly every endorsement from the state’s newspaper editorial pages.

Smith hewed closely to Republican Party-line positions, but had signaled that on key fiscal issues that he would break to the conservative side of party leaders in Congress. Smith attacked Casey as a do-nothing career politician, the right hand of Obama and an overreaching and overspending government that is stifling entrepreneurship and the development of fossil fuels such as coal.

The campaign was Smith’s first statewide after many years of giving heavily to Republican causes while running his coal-mining business. He started his own tea party group in 2009, sold his coal-mining businesses in 2010 and, after spending his life as one of western Pennsylvania’s many conservative Reagan Democrats, he switched his registration in 2011 to Republican. He won the party’s nomination by spending more than $5 million of his own money to win a five-way race that included a candidate endorsed by the state party and Gov. Tom Corbett.