SYDNEY—Australia’s conservative government eked out a surprise victory in Saturday’s national elections after voters in resource-rich districts turned against center-left opponents who had put climate change at the heart of their campaign.

Behind in polls for more than two years, Prime Minister Scott Morrison’s Liberal-National coalition appealed to voters in battleground states such as Queensland, struggling at the end of a long mining boom, with a campaign focused on the economy and jobs.

With...