With 226 seats in the 230-seat parliament awarded in the official count, the Socialists reached 106 seats, compared with 77 for the second-place Social Democratic Party.
The parliament chooses a government, usually taken from the party that has the most lawmakers.
The Socialists, however, fell short of a parliamentary majority. They could take power as a minority government or pursue alliances with other left-of-center parties to pass legislation — as they did in their last term with the Portuguese Communist Party and the radical Left Bloc.
Antonio Costa, the Socialist leader and incumbent prime minister, said he would start talks with other parties with the aim of securing a political agreement for the next four years.
"Political stability is essential for Portugal's international credibility," Costa said in a victory speech.
He said his prospective government will continue its efforts to improve debt-heavy Portugal's public finances — a key achievement in his first term that endeared him to the Portuguese and to other European leaders keen to avoid a repeat of the continent's recent financial crisis.
As other Socialist parties have lost ground across Europe in recent years, Costa's administration won respect by proving wrong the doubters who said it would overspend.
The Left Bloc, which had won 19 seats in the incomplete count, said it was ready to open negotiations with the Socialists on a government program.
The Portuguese Communist Party had 10 seats and the Christian Democrats five.
Small fringe parties also picked up a handful of seats, and some of them could be targets for the government to recruit support in parliament.
Costa took power four years ago on a promise to undo austerity measures introduced during Europe's financial crisis, when Portugal needed an international bailout.
Now his Socialists reaped the rewards of Portugal's economic rebound in recent years. Annual growth climbed from 0.2% in 2014 to 2.1% in 2018, and unemployment dropped by around half, to 6%, over that period.
The budget deficit — long a torment for Portugal as governments routinely overspent — is now close to zero under the financial stewardship of Finance Minister Mario Centeno, who also chairs the meetings of finance ministers from the 19 European nations that used the shared euro currency.
Centeno is expected to continue in the post.
The Social Democrats are still associated in the public mind with deep cutbacks and a three-year recession that ended in 2014, when they were in government. Also, party leader Rui Rio has struggled to contain disagreements between factions of his party.
Portugal hasn't witnessed the rise of populist or far-right parties which have risen to prominence elsewhere in the EU. Protests in Portugal are traditionally channeled into a low turnout — only about half the country's 10.8 million eligible voters cast their ballot.
The next government faces plenty of challenges, including the United Kingdom's impending departure from the EU. The U.K. is one of Portugal's main export markets.
While government spending is now more in line with revenues, Portugal's government debt is equivalent to more than 120% of gross domestic product — the third highest in the EU. And the combined government, private and corporate debt is more than three times the country's GDP, the Bank of Portugal says. If European interest rates rise, the Portuguese could find themselves in big trouble.
Other challenges include climate change, as rainfall in the southern half of the country dwindles, and an aging population that threatens the financing of the welfare system. The EU says that at current fertility rates the number of Portuguese will decline from 10.3 million this year to 6.6 million in 2100.