The center-right bloc, meanwhile, got 39.3 percent of the vote. That was a slimmer gap than any predicted by opinion polls during the campaign, and one that points to a period of protracted uncertainty as the parties jostle for position in the government while distancing themselves from the far right.

Feminist Initiative, a party that campaigned on a platform of equality and antiracism, had appeared poised to surpass the 4 percent threshold necessary to enter Parliament, potentially strengthening a center-left coalition. However, provisional results showed the party with only 3.1 percent.

Despite steering the country to a rapid and sustained recovery from the global economic slump of 2008, the Moderate Party of Mr. Reinfeldt, 49, saw its support fall away to 23.2 percent, as Swedes grew anxious about the consequences for Sweden’s cherished welfare system with the government decreasing spending and expanding the role of the private sector.

“The gold rush is over,” Mr. Lofven repeated during the campaign, tapping into a widespread feeling that excessive profits reaped by “rogue companies” were taking precedence over the quality of services in schools and elderly care. He has said that as prime minister, within 100 days he would launch a “reform package” for schools and set up a commission of inquiry into financing the welfare system.

The center-right parties have accused the left of threatening to squander the economic gains of the past eight years and promising spending that cannot be financed responsibly. Money was so tight, Mr. Reinfeldt said at the start of the campaign, that his party could promise almost no increases in welfare expenditures. He said that was partly because of the “vast cost” of integrating tens of thousands of refugees from Syria, Somalia and Iraq.

This high-profile move to link public spending cuts with immigration was seized upon by the far-right Sweden Democrats, who made a 90 percent cut in refugee numbers a central plank of their campaign.

Their persistent message has tempted Swedes uneasy with the pace of immigration and worried about the strains on schools and housing as the country copes with numbers of refugees not seen since the Balkan wars two decades ago.

Almost a third of voters for the Sweden Democrats on Sunday said they had backed the Moderate Party in the last election four years ago, according to exit polls by Sweden’s public sector broadcaster.