The right-wing populist Law and Justice party is on course to win most seats in Poland's general election, an exit poll suggests, but is unlikely to secure a third term in office.
Known as PiS, it is set to win 36.6% of the vote, with the centrist opposition on 31%, says the Ipsos poll.
If that is correct, Donald Tusk's Civic Coalition has a better chance of forming a coalition.
He is aiming to end eight years of PiS rule under leader Jaroslaw Kaczynski.
The PiS leader admitted he did not know if the party's "success will be able to be turned into another term in power".
Initial results give PiS the lead, but they reflect small towns and the countryside which are party strongholds. A second Ipsos poll published on Monday morning reinforced its first poll on Sunday evening.
"Poland won, democracy has won," Mr Tusk, 66, told a large crowd of jubilant supporters in what felt like a victory rally in Warsaw. "This is the end of the bad times, this is the end of the PiS government."
There were roars as the Ipsos poll flashed up on the screen and Mr Tusk appeared to loud cheers and chants of his name.
Supporters appeared stunned, and election officials said later that turnout was probably 72.9%, the highest since the fall of communism in 1989.
Polls closed at 21:00 local time, but there were still queues of voters reported well into the night in Warsaw and Krakow, and into the early hours in Wroclaw.
A larger proportion of 18-29 year-olds had turned out to vote than over-60s, Ipsos said.
PiS was heading for 198 seats in the 460-seat Sejm or parliament, according to the later poll, and would fall short of the 231 needed for a majority. It is unlikely to have much help from the far-right Confederation party, whose leader admitted it had fared far worse than expected, with a predicted 14 seats.
Mr Kaczynski has painted his rival as a puppet of Berlin and Brussels and vowed to maintain his party's strong anti-migration policies.
Civic Coalition leader Donald Tusk has described the vote as Poland's most important since the fall of communism and vital for its future in the European Union.
He has vowed to improve relations with the EU and unlock €36bn (£30bn) of EU Covid pandemic recovery funds frozen in a row over PiS judicial reforms that led to staffing top courts with judges sympathetic to the ruling party.
If the two exit polls are borne out, Mr Tusk's party has more chance of forming a broad coalition, with centre-right Third Way and left-wing Lewica.
There were few smiles among PiS party faithful in the minutes before the close of polls.
"We have to hope," Mr Kaczynski declared. "Regardless of whether we are in power or whether we are in opposition, we will implement this project in various ways and we will not allow Poland to be betrayed."
PiS supporters put on a brave face, chanting "Jaroslaw" and waving Polish flags, as the later exit poll suggested they had lost 37 seats since the 2019 election.
A party spokesman told the BBC he was still hopeful of forming a government as the poll was just a prediction.
Queues formed outside polling stations across Poland and beyond on Sunday.
A marbled foyer in Warsaw's Stalinist Palace of Culture was crammed with voters, who snaked out into the square outside.
"The campaign was very strong and emotional, that's why there are so many people," a PiS voter called Agnes told the BBC.
One result of Poland's ferocious election campaign was the increased turnout. "It seems that we beat the turnout record," Commission head Sylwester Marciniak told a news conference.
Many voters in central Warsaw came with children and even pets, and election officials and security guards helped elderly voters climb the steps.
Voters talked of being nervous of the result of the election, and all of them saw it as decisive for the future direction of Poland.
Whoever wins, Poland's strong support for Ukraine is unlikely to change, almost 20 months into Russia's full-scale invasion. However, PiS leaders showed signs of wavering in recent weeks, in an apparent bid to bring back voters attracted to the Ukraine-sceptic Confederation party.
"We have a war on our border. We have to be sure the government will take us in the right direction and be more resistant to Russia," said another voter called Ela.
Poles voted in more than 30,000 polling stations, and there were long queues outside Poland too, with 600,000 expats registered to vote.
"They're the most important elections I've voted in during my lifetime," said Magdalena Bozek as she queued up to vote in London. "It's been quite a difficult eight years for us, for pro-Europeans."
Civic Coalition has also vowed to liberalise abortion laws, after a near-total ban imposed in 2021.
The centre-right Third Way appeared to be one of the big winners of the night, with a predicted 13.5% of the vote, promising to simplify taxes and offering an alternative to the two big parties.
Piotr Buras, from the European Council on Foreign Relations, said an opposition victory would open the way to a "massive reorientation" of domestic and European policy. Their immediate goal would be to remove PiS figures from state institutions and public TV, he added.
Poland is divided into 41 districts and has a proportional representation system for its parliament, based on party lists. Expat votes count towards the Warsaw district.
President Andrzej Duda, an ally of the socially conservative ruling party, would normally ask the biggest party to form a government, and his aide indicated that was the traditional next step.
But if PiS fails to win a vote of confidence, then parliament would appoint a new prime minister who would then choose a government and also have to win a confidence vote in the Sejm .
That would leave PiS as Poland's caretaker government potentially into December.
Five parties are set to cross the 5% threshold and enter the 460-seat Sejm or parliament.
Poles also voted for the upper house, the Senate, and taking part in four referendums that all appeared designed to bring PiS voters out to vote.
One asked whether the retirement age should increase, another whether Poland should accept more migrants from the rest of the EU.