"I have always believed in miracles," Prime Minister Scott Morrison told a jubilant Sydney crowd.
He compared his Liberal Party's victory for a third three-year term to the births of his daughters, Abbey, 11, and Lily, 9, who were conceived naturally after 14 years of in vitro fertilization had failed. His wife, Jenny Morrison, suffered endometriosis.
"I'm standing with the three biggest miracles in my life here tonight, and tonight we've been delivered another one," he said, embraced by his wife and daughters.
Opposition leader Bill Shorten had earlier conceded defeat as the coalition came close to a majority in the 151-seat House of Representatives, where parties need a majority to form a government. Vote counting was to continue on Sunday.
"I'm disappointed for people who depend upon Labor, but I'm glad that we argued what was right, not what was easy," Shorten told his supporters.
Shorten would have become Australia's sixth prime minister in as many years. He said he would no longer lead Labor after six years at the helm.
The tight race raised the prospect of the coalition forming a minority government. The conservatives became a rare minority government after they dumped Malcolm Turnbull as prime minister for Morrison in an internal power struggle last August. The government then lost two seats and its single-seat majority as part of the blood-letting that followed.
An unpopular single-term Labor government that was voted out in 2013 had been the only previous minority government since World War II.
Opinion polls prior to Saturday's election had suggested that the coalition would lose and that Morrison would have had one of the shortest tenures as prime minister in the 118-year history of the Australian federation.
Morrison had focused his campaigning on polling that showed while Labor was more popular than the government, the prime minister was more popular than Shorten.
There was so much public confidence of a Labor victory that Australian online bookmaker Sportsbet paid out 1.3 million Australian dollars ($900,000) to bettors who backed Labor two days before the election. Sportsbet said 70% of wagers had been placed on Labor at odds of $1.16.
Another betting agency, Ladbrokes, said it had accepted a record AU$1 million wager on Labor.
Shorten, who campaigned heavily on reducing greenhouse gas emissions, said Saturday morning that he was confident Labor would win, but Morrison would not be drawn on a prediction.
Morrison is the conservatives' third prime minister since they were elected in 2013.
Tony Abbott, who became the first of those three prime ministers in the 2013 election, conceded defeat in the Sydney seat he has held since 1994.
Polling suggests climate change was a major issue in that seat for voters, who instead elected an independent candidate, Zali Steggall. As prime minister in 2014, Abbott repealed a carbon tax introduced by a Labor government. Abbott was replaced by Turnbull the next year because of poor opinion polling, but he remained a government lawmaker.
A maverick senator who blamed the slaughter of 51 worshippers in two New Zealand mosques on the country's immigration policies also lost his bid for election.
Fraser Anning was the target of widespread condemnation for railing against Muslim immigration within hours of the mass shootings in the New Zealand city of Christchurch in March. He faced more criticism later for physically striking a teenage protester who cracked a raw egg on his head and was censured by the Senate.
Senior Labor lawmaker Chris Bowen said his party may have suffered from what he conceded was an unusual strategy of pushing a detailed policy agenda through the election campaign.
Morrison began the day Saturday by campaigning in the island state of Tasmania, where the Liberals appeared to have gained two Labor-held seats. He then flew 900 kilometers (560 miles) home to Sydney to vote and to campaign in Sydney seats.
Shorten campaigned hard on more ambitious targets to reduce Australia's greenhouse gas emissions.
The government has committed Australia to reduce its emissions by 26% to 28% below 2005 levels by 2030. Labor has promised a 45% reduction in the same time frame.
Shorten, a 52-year-old former labor union leader, has also promised a range of reforms, including the government paying all of a patients' costs for cancer treatment and a reduction of tax breaks for landlords.
Morrison, a former tourism marketer, promised lower taxes and better economic management than Labor.
Both major parties promised that whoever won the election would remain prime minister until he next faces the voters' judgment. The parties have changed their rules to make the process of lawmakers replacing a prime minister more difficult.
During Labor's last six years in office, the party replaced Prime Minister Kevin Rudd with his deputy Julia Gillard, then dumped her for Rudd.
Associated Press writer Trevor Marshallsea in Sydney contributed to this report.
Copyright 2019 The Associated Press. All rights reserved. This material may not be published, broadcast, rewritten or redistributed.