(AFP) Frederiksen's five-party "red" bloc had looked set to lose its majority as vote counting wore on throughout Tuesday evening, but as the last votes were tallied, the bloc eked out the 87 seats it needed in mainland Denmark.
Together with another three seats from the autonomous overseas territories of the Faroe Islands and Greenland, the bloc holds a total of 90 of parliament's 179 seats.
Opinion polls had predicted a historically weak election for the Social Democrats, but they instead gained two seats compared to the 2019 election, winning 27.5 percent of votes.
"Social democracy had its best election in over 20 years," Frederiksen said in a speech to campaign supporters early Wednesday.
"We are a party for all of Denmark," she added.
The right-wing "blue" bloc -- an informal liberal and conservative alliance supported by three populist parties -- won 72 seats in mainland Denmark and one in the Faroe Islands.
Broad government
The photo-finish victory scuppered the hopes of a newly-created centrist party, the Moderates, of playing the role of kingmaker -- an outcome that had looked likely until Frederiksen secured a majority.
The party was created only months earlier by former two-time prime minister Lars Lokke Rasmussen, who looked set to once again return to the centre of Danish politics following a campaign in which both the left and right had competed for his favour.
Polling at barely two percent of voter support two months ago, the Moderates won more than nine percent of votes, and Lokke Rasmussen insisted he wanted to be "the bridge" between the left and right.
"It's not red or blue, it's about common sense," he told cheering supporters in a speech Tuesday evening, while declaring that a new government was a certainty.
During the campaign, Frederiksen floated the idea of a left-right coalition government led by herself, and said she was willing to discuss healthcare reform -- Lokke Rasmussen's main campaign issue.
With the left majority secured, Frederiksen reiterated on Wednesday that she hoped to form a broad government.
"When the Social Democrats say something, it is something we follow through on," the 44-year-old prime minister said.
She said the current government would formally resign on Wednesday in order to start the process of forming a new administration.
After coming to power in 2019, Frederiksen has embodied Denmark's newly styled Social Democrats, embracing restrictive migration policies in the name of protecting the welfare state.
'Mink crisis'
While her government was largely hailed for its handling of the Covid-19 pandemic, the election ended up being triggered by the "mink crisis".
The affair has embroiled Denmark since the government decided in November 2020 to cull the country's roughly 15 million minks over fears of a mutated strain of the novel coronavirus.
The decision turned out to be illegal, however, and a party propping up Frederiksen's minority government threatened to topple it unless she called elections to regain the confidence of voters.
The election campaign was dominated by climate concerns, inflation and healthcare.
Since the anti-immigration far-right entered parliament in the late 1990s, Denmark has championed ever-stricter migration policies.
Advocating a "zero refugee" policy, the Social Democrats government is working on setting up a centre to house asylum seekers in Rwanda while their applications are processed.
As most parties back the restrictive policies, the issue is rarely up for debate.
The election also saw the return to politics of former migration minister Inger Stojberg after she was ousted from parliament following a criminal conviction for a decision she made as a member of Lokke Rasmussen's cabinet.
Stojberg was found guilty of flouting her ministerial duties and violating human rights when she ordered the separation of asylum-seeking couples in which the woman was under 18, a measure she said was designed to combat forced marriages.
But after completing her two months of house arrest with an electronic bracelet, she founded the Denmark Democrats, who managed to secure over eight percent of votes on a platform of less centralisation, less influence from Europe and fewer immigrants