DÜSSELDORF, Germany — The party of Chancellor Angela Merkel scored an upset victory on Sunday in elections in Germany’s most populous state that were seen as a dress rehearsal for national parliamentary elections in September, when she will seek a fourth term.

The victory in North Rhine-Westphalia, home to 18 million people and one in five German voters, dealt a severe blow to Martin Schulz, Ms. Merkel’s Social Democratic challenger, who admitted to a bitter defeat in his home state, traditionally the leftists’ heartland. Cheers erupted at the state headquarters of Ms. Merkel’s Christian Democratic Union when early results came in.

With almost all ballots counted, the Christian Democrats had won almost 33 percent of votes cast, compared with around 31 percent for the center-left Social Democrats.

The national elections are still more than four months away — a long time in an era of febrile politics across Europe, where mainstream parties have seen their grip weaken in recent elections. But Ms. Merkel, in power since 2005, seems to be bucking the trend, buoyed by experience, a calm temperament and the exceptional economic strength of Germany, which has 4.1 percent unemployment and just last week announced record exports and tax revenues that will exceed expectations by 55 billion euros by 2020.