Mr. Turner’s victory stirred anxiety among Democrats already worried about elections next year for president, the House and the Senate. The Turner campaign had eagerly courted disenchanted Democrats, and outside polling places around the district on Tuesday, multiple longtime Democrats confessed that despite concern about Mr. Turner’s eagerness to slash federal spending, they chose him hoping that his election would get lawmakers’ attention.

“I am a registered Democrat, I have always been a registered Democrat, I come from a family of Democrats — and I hate to say this, I voted Republican,” said Linda Goldberg, 61, after casting her ballot in Queens. “I need to send a message to the president that he’s not doing a very good job. Our economy is horrible. People are scared.”

Mr. Turner will become the first Republican since 1920 elected to represent the Ninth Congressional District, which now stretches from the Rockaways to Forest Hills and encompasses a swath of middle-class and working-class neighborhoods in Brooklyn and Queens. The district is predominantly white and has long been known for its large Jewish population, though it has become increasingly diverse in recent years.

Mr. Weprin, 55, a former city councilman and son of an Assembly speaker, was nominated by local party leaders to run after Mr. Weiner, a Democrat, resigned in June following his admission that he had sent sexually explicit messages to women he had met online.

Mr. Turner, 70, ran against Mr. Weiner in the last election but has never held office. He mounted a surprisingly forceful campaign, painting Mr. Weprin as a party insider who would support the status quo.

Mr. Turner capitalized on discontent in some corners of the Jewish community with Mr. Obama’s posture toward Israel and his handling of the Middle East peace process. Former Mayor Edward I. Koch, a Democrat, urged voters to rebuke the president by voting for Mr. Turner.