Duda got 51.6 percent of the vote to the sitting president’s 48.4 percent, according to final results released Monday afternoon. Komorowski didn't wait for the final count, conceding defeat just after the polls closed at 10:30 on Sunday night.

“It didn’t work this time. That’s how the voters in free and democratic Poland decided,” Komorowski said. “I wish him a successful presidency because I wish Poland well,” he said, offering his congratulations to Duda.

“Those who voted for me wanted change, and I thank them for that,” a serious-looking Duda told his cheering supporters.

This dark horse victory upends more than eight years of political dominance by the centrist Civic Platform (PO) party, which had backed Komorowski. Duda comes from the right-wing Law and Justice (PiS), which goes into the coming campaign for the more powerful institution of parliament this fall with greater confidence and unexpected momentum.

Prime Minister Ewa Kopacz came into office only last year after Civic Platform founder Donald Tusk left to take the presidency of the European Council in Brussels. Lacking charisma and facing rising dissent from within the party, she faces a hard climb to get the ruling party a third consecutive electoral victory.

The change at the presidential palace in Warsaw carries possibly significant implications for Poland’s foreign policy. Under Kopacz and especially her predecessor Tusk, Warsaw became one of Berlin’s closest allies in the EU and a leading player in Brussels. Although the president is responsible for foreign relations together with the government, a Duda administration is likely to be more pro-American and more Euroskeptic, and less enthusiastic about nurturing close ties with Germany.

Duda’s victory is a jolt to pundits and pollsters. A few weeks before the May 10 first round that Duda narrowly won, opinion surveys showed Komorowski well ahead. A few months ago he had the support of about two-thirds of voters, and his reelection was seen as a foregone result.

This upset came as a result of skillful campaigning by Duda and, perhaps more importantly, rising dissatisfaction with the long reign of Civic Platform, especially among younger voters.

Duda was little known before he was chosen to be his party’s standard-bearer by PiS founder and former prime minister, Jarosław Kaczyński. He initially seemed to be a no-hoper against the generally popular Komorowski. But the incumbent ran a sluggish campaign, while Duda hunted for votes around the country. Komorowski was more aggressive in the last two weeks, but he was unable to make up the gap against Duda.

Duda was an energetic presence on the hustings. At 43 he is two decades younger than Komorowski, and was a fresh face on a political scene dominated by many of the same people who have ruled Poland since the end of communism in 1989. Komorowski cut his political teeth in opposing the communists, even serving a brief jail term in 1980. Kaczyński came of age in the same era.

Duda is Poland’s first president to have largely grown up after the fall of communism.

The youthful lawyer managed to tap into the fatigue with the incumbent party as much as the president. Unofficial exit poll results showed that he had taken about 60 percent of voters younger than 30.

That sense of grievance may seem paradoxical in one of Europe’s most successful economies. Poland is the only European country to have avoided a recession in more than two decades. During PO’s two terms in office, per capita GDP (measured in purchasing power parity) has grown from $16,877 in 2007 to $23,649 in 2013, according to the World Bank. Unemployment has fallen as well and now stands at 9 percent.

By all accounts, this is the most prosperous and secure period in Poland’s troubled history. But in smaller towns and villages there is little work and salaries are low. More than two million from this so-called “Polska B,” the other Poland far from the thriving cities, have left to find jobs in western Europe.

Komorowski stumbled during the campaign when he was asked how someone making €500 a month (higher than the minimum wage) could hope to afford an apartment. “Find another [job]. Take a loan,” came his response. “Do you know that unemployment in Poland is falling, while in England it’s rising?” He came off tone deaf.

The initial beneficiary of that discontent was Paweł Kukiz, a rock musician and social activist who came in third with a fifth of the vote during the first round by running a campaign aimed at the political establishment. More of those voters chose Duda over Komorowski in Sunday’s second round.

“Voters demanding political change seem to have swung the political pendulum,” said Wawrzyniec Smoczyński, managing director of Polityka Insight, a political analysis firm.

With Duda at the 17th century presidential palace in Warsaw, his party can try to seize the political initiative before the parliamentary elections. As president, Duda will be able to veto laws, potentially blocking most of Kopacz’s agenda. He can also propose legislation.

Aleksander Smolar, head of the Warsaw-based Stefan Batory think tank, said he expected him to try to make good on his populist promises like lowering the retirement age, which would undo one of the hallmark economic policies of Tusk’s government, and propose increasing subsidies for large families.

“These will probably be rejected by the parliament, but they are popular ideas and will help PiS in the election,” said Smolar. “All of this will increase the already evident disarray in the government camp.”

Duda has promised to follow in the footsteps of Hungary’s Prime Minister Viktor Orbán, who battled foreign banks over the issue of ruinous mortgages denominated in Swiss francs. About half a million Poles have such loans. They tend to be urban white-collar Poles, normally Civic Platform’s core voters, but seemingly a helping hand from Duda could change that.

While Orbán serves as a model of domestic policy for PiS — one of the party’s mottos has been “Budapest on the Vistula” — the Hungarian prime minister’s stroppy relations with the EU are unlikely to be followed by a Duda administration. During his campaign, Duda did talk of Poland “not being in the mainstream” of the EU, and he has been slightly more conciliatory towards Moscow than Komorowski was — although he too wants a permanent NATO base on Polish soil. But he was measured in his discussion of EU affairs. What is likely to happen is a cooling of ties with Berlin, reflecting PiS’s traditional suspicions of Germany.

“It won’t be like under Tusk when there was a special place for Germany in Polish politics,” said Smolar.

Until now Poland had been a loyal German ally in the EU. But Duda's views are more similar to those of the UK. Like the Tories, which together with PiS forms the core of the European Conservatives and Reformists Euroskeptic grouping in the European Parliament, he is not keen on Poland joining the euro, and is suspicious of federalist European projects.

Turnout on Sunday was 55.4 percent, up from less than 49 percent in the first round, a record low in any presidential election since Poland became a democracy in 1989.

Listen to the Polish edition of POLITICO's In the loop podcast in which Matthew Kaminski speaks to Jan Cienski on the impact of Duda's victory on Poland and Europe.