Julia Gillard will stay as Australia's prime minister after winning the backing of two key independent MPs.
Tony Windsor and Rob Oakeshott held the balance of power in parliament after a fellow independent MP, Bob Katter, backed opposition leader Tony Abbott.
The dramatic announcement ended more than two weeks of political deadlock following indecisive elections.
It gives Ms Gillard's Labor Party the backing of one more MP in the lower house than the Liberal-led coalition.
The minority government is Australia's first since World War II.
"The events of the past fortnight show us unequivocally that our democracy is very, very strong indeed," Ms Gillard told a news conference in Canberra.
"With today's agreement... Labor is prepared to deliver stable, effective and secure government for the next three years. Ours will be a government with just one purpose - to serve the Australian people."
"We will be held more accountable than ever before, and more than any government in modern memory," she added.
Mr Abbott told reporters he would respect the outcome, despite his alliance having won one more seat than Ms Gillard's party on 21 August.
"The Coalition won more votes and more seats than our opponents, but sadly, we did not get the opportunity to form a government," he said. "Obviously I'm disappointed about that, but that's our system."
'Three amigos'
The BBC's Nick Bryant in Sydney says the election was often compared to a soap opera and ended like the finale of a reality show, with the winner kept a secret until announced live on national television.
In the end, the final arbiters were three country-based MPs - dubbed the "three amigos" - who negotiated and deliberated for 17 days.
Shortly after lunchtime, Bob Katter from North Queensland stepped before the cameras to announce he had backed the Liberal-led coalition.
He said he was angry with the treatment of his fellow Queenslander, Kevin Rudd, who was ousted as prime minister by Ms Gillard in an internal party coup in June. She had been Mr Rudd's deputy.
"Kevin's thinking and my thinking are very similar," Mr Katter said. "I'm very good friends with him."
Then an hour later, Mr Windsor announced his support for Ms Gillard.
He said Labor's plans for a national broadband network and its position on climate change had been major factors in his decision, as well as a feeling that if he supported Mr Abbot he would rush to the polls.
Mr Windsor also made a plea to his conservative constituents to co-operate with the new minority government, saying: "This is about using the political system to advantage the people we represent."
It all came down to Mr Oakeshott, who revealed at the end of a 20-minute speech that Ms Gillard would remain as prime minister.
Mr Oakeshott stressed how close the call had been, detailing a lengthy list of meetings and consultations the independents had undertaken with Labor, the Liberals and other key players.
He said he taken into account who could form a more stable government, who had a better deal for rural Australia and who could work best with the Senate, where the Greens will hold the balance of power.
"This is not a mandate for any government. We should have a great big swear jar in this building for the next three years and if anyone uses that word 'mandate' they should have to chip in some money."
"This parliament is going to be different; no one party has dominance over the executive or the parliament. That is a reality of the way we're going to do business for the next three years. And that is a good reality."
The independents' backing means Ms Gillard will be able to press ahead with her plans to introduce the broadband network, a 30% tax on iron ore and coal mining companies' profits, and a tax on major polluters to help cut carbon emissions by 5% by 2020.
But our correspondent says that with such a tiny majority, the government is bound to be hostage to any unexpected events such as by-elections.