It boiled down to a choice between two different visions of the future: one dominated by nationalism, traditional Catholic norms and the defense of Polish sovereignty; the other by promises to “bring Poland back to Europe” and the liberal democratic values espoused by the European Union.
In the end, after a long, vicious election campaign in a highly polarized country, opponents of the nationalist governing party won a clear majority of seats in a pivotal general election held on Sunday, according to final official results Tuesday.
That victory opened the way for a potentially drastic shift away from Poland’s deeply conservative policies at home and its role abroad as a beacon for right-wing groups and politicians opposed to liberal values.
The prospect of an end to years of testy relations between Warsaw and Brussels delighted Polish liberals and those elsewhere worried by what had, for a time, seemed like a rising tide of right-wing, and sometimes left-wing, populism in Poland and across Europe.
The election, cast by both sides of the political divide as Poland’s most consequential vote since voters rejected communism in 1989, offered a multitude of parties from the far right to the progressive left.
“These are absolutely historic moments,” Donald Tusk, Civic Coalition’s leader, told euphoric supporters in Warsaw on Tuesday. “The weather has changed,” he added before repeating a line from a popular song often used during the campaign: “It’s time for a happy Poland.”
Held just two weeks after voters in neighboring Slovakia handed victory to a Russia-friendly party tainted by corruption, the Polish election was closely watched as a gauge of Europe’s direction.
It was also seen as a measure of whether Hungary, increasingly authoritarian under Prime Minister Viktor Orban, would remain an idiosyncratic outlier or become the standard-bearer of a growing cause whose friends extend beyond ideological allies like the TV personality Tucker Carlson, a big fan of Mr. Orban, to include European governments.
Hungary and Poland for a time were close partners, leading what they promoted as a European renaissance rooted in Christian values and national sovereignty, but they parted ways over the war in Ukraine. Mr. Orban tilted toward Moscow while Poland offered robust support for Ukraine, though that position wobbled somewhat during the election campaign.
Official results confirming exit polls released on Sunday cast gloom over the governing party, Law and Justice, which had fought the election on promises to save Poland from European bureaucrats pushing “L.G.B.T. ideology” and what it denounced as Germany’s hegemonic aspirations.
A final tally of votes released on Tuesday by the electoral commission gave Civic Coalition, the main opposition party, and two smaller groups also opposed to the Law and Justice party — Third Way and New Left — 248 seats in the 460-member Sejm, the more powerful lower house of Parliament.
Together they won 53.7 percent of the vote after a record turnout of about 74 percent, compared with 35.4 percent of ballots cast for Law and Justice. That tally would most likely reduce Law and Justice’s presence in the Sejm by 33 seats.
Arkadiusz Mularczyk, of the Law and Justice party, acknowledged defeat, saying that “we cannot be offended by democracy” and that, “after eight difficult years in government, perhaps it is time for the opposition.”
Poland remains deeply divided by generation and geography, with Law and Justice sweeping poorer rural areas in the south and east while Civic Coalition, its main rival, strengthened its grip on urban centers like Warsaw and richer areas in the center and west.
But, reversing a trend across Europe toward increased youth disenchantment with electoral politics of all ideological shades, Poles under 29 voted in larger numbers than voters over 60. That was despite the two main rival camps being led by veterans — Jaroslaw Kaczynski, 74, the Law and Justice chairman, and Mr. Tusk, 66, the leader of Civic Coalition, both former prime ministers.
The opposition also won a large majority of seats in the 100-member Senate, the upper house, but its victory in both chambers of Poland’s Parliament, though a big symbolic boost for supporters of liberal democracy and European integration, will be crimped by its having to work with a Polish president loyal to Law and Justice.
The president, Andrzej Duda, an outspoken critic of Mr. Tusk in the past, will stay in office until elections in 2025 and, until then, can veto legislation passed by his political opponents in Parliament. Mr. Duda is now responsible for asking someone to form a government, a task that will probably fall, at least initially, to a member of Parliament from Law and Justice, which won more votes than any other single party. Without a majority, Law and Justice is unlikely to succeed and Mr. Duda will need to turn to the opposition.
Anatol Magdziarz contributed reporting.