Four years later, the “geringonça” label has been worn as a badge of honor by Mr. Costa. He has become a rare thing in Europe — not only a Socialist head of government, but one who has overseen a solid economy, helped by tourism and foreign investors, and proved wrong the critics who had long caricatured the left as incapable of fiscal discipline.

Mr. Costa, 58, had been leading in the polls long before the voting on Sunday, making a Socialist victory appear as almost a foregone conclusion.

Still, as results trickled in on Sunday, they suggested a resounding defeat for the prime minister’s center-right opponents. They had maintained that Mr. Costa was doing little more than taking the credit for an economic recovery that he did not engineer. They argued that it was instead rooted in earlier spending cuts and reforms imposed by international creditors in granting a bailout of 78 billion euros, or about $85 billion, to Portugal during the financial crisis.

In late 2015, Mr. Costa promised to put an end to the austerity of the bailout program. But instead, he showed himself to be a firm believer in fiscal discipline, even squeezing investment in sectors like health care to bring Portugal close to a balanced budget.