LISBON—Portugal’s president on Tuesday appointed anti-austerity Socialist leader António Costa as prime minister in a move that ends weeks of political turmoil but opens the door to a period of economic uncertainty.

The ascent of 54-year-old Mr. Costa, the former mayor of Lisbon, makes Portugal the second eurozone country this year after Greece to rebel against a belt-tightening recipe imposed on troubled Southern European economies by the European Union’s executive arm. Greece’s rebellion, which started with the election...