CHICAGO  Stunning many who considered the district west of Chicago reliably Republican territory, Bill Foster, a physicist and Democrat, won a special election on Saturday to fill the Congressional seat that J. Dennis Hastert, the former speaker of the House, held for two decades.

Mr. Foster’s success deeply disappointed Republicans, in part for its broader implications: the victory in this early race may buoy Democrats as they look ahead to a string of Republican retirements this fall.

Mr. Hastert was one of the best-known Republican members of Congress and he was elected from what was once a Republican stronghold.

Mr. Foster defeated James D. Oberweis, a dairy company owner, in a contest for the 14th Congressional district, a long swath that runs from the far western suburbs of Chicago nearly to the state’s Iowa border. He will fill the remaining months of Mr. Hastert’s term.