Australia's first female prime minister, Julia Gillard, has been sacked by her party just months before the next election and replaced by the man she ousted three years ago.

After an unprecedented day of political bloodletting in Canberra, Kevin Rudd beat Gillard in a ballot of MPs by a margin of 57 to 45 to become Labor party leader. It is only the second time a sitting Australian prime minister has been removed from their first term in office by their party; Rudd was the first.

Delivering his victory speech, Rudd dedicated himself to winning the election in September. "In 2007 the Australian people elected me to be their prime minister. That is a task that I resume today with humility, with honour and with an important sense of energy and purpose."

He also paid tribute to Julia Gillard. "She is a woman of extraordinary intelligence, great strength, great energy. All of you across the nation would recognise those formidable attributes," he said. "Julia has achieved much under the difficult circumstances of minority government, helped by a dedicated set of ministers."

"She has been a remarkable reformer and I acknowledge those contributions this evening," he said.

Rudd said he had taken on the challenge of the leadership because he could not "stand idly by and allow a (conservative) Abbott government come to power in this country by default".

Bill Shorten announces he is backing Kevin Rudd. Photograph: Mike Bowers

Gillard laid down the challenge to Rudd when she called on Wednesday for a do-or-die ballot, on the condition that the loser retire from parliament to end the debilitating Labor leadership war.

Shortly after the vote Gillard confirmed she would not contest the next election. A cabinet reshuffle is expected and a number of former Gillard ministers have already tendered their resignations.

In her concession speech, Gillard congratulated Rudd and called on the party to fight to win the September election. She acknowledged that her tenure as prime minister had been difficult, saying: "In the years in which I've served as prime minister, predominantly I've faced a minority government and political division in my own party. It has not been an easy parliament to operate in."

Gillard also reflected on her role as the country's first female prime minister. "The reaction to being the first female PM does not explain everything about my prime ministership, nor does it explain nothing about my prime ministership," she said.

"I've been a little bit bemused by those colleagues in the newspapers who have admitted that I have suffered more pressure as a result of my gender than other PMs in the past but then concluded it had zero effect on my political position or the political position of the Labor party."

With tears in her eyes, she talked about what her term as prime minister might mean for other female leaders: "What I am absolutely confident of is it will be easier for the next woman, and the woman after that, and the woman after that, and I'm proud of that."

After her press conference, she went to tender her resignation to the head of state and the Queen's representative in Australia, the governor general.

Kevin Rudd confirms his leadership challenge. Photograph: AAP

Conservative opposition leader, Tony Abbott, whose party is well ahead in the polls, criticised the revolving door of Labor leadership, saying Australians deserved better.

"In 2007 you voted for Kevin and got Julia," he said. "In 2010 you voted for Julia and got Kevin. If you vote for the Labor party in 2013 who knows who you will end up with."

He called on Rudd to hold to an election immediately, rather than wait until September.

"It's now for the Australian people to determine who the prime minister is," Abbott said.

The day of high drama began when supporters of Rudd, who had advocated his return to the leadership for the past three years of the hung parliament, began circulating a petition to try to force a contest in this, the last sitting week of parliament before the September election.

Within hours, Gillard went on the attack and made the decision to hold a snap vote on her position. "It is in the best interest of the nation and the Labor party for this to be resolved," she said. "This is it. There are no more opportunities, tonight's the night."

Wednesday's change of leader follows months of speculation, during which Gillard made clear she would not stand down despite opinion polls that repeatedly showed Rudd to be the more popular leader.

With the party's support dwindling to about 30%, and the prospect of Labor losing at least half of its parliamentary seats, she stood firm while Rudd's backers plotted.

Once Gillard called the leadership ballot – the third time she had agreed to contest her position in as many years – Rudd made his pitch to return, saying Labor was heading for a catastrophic defeat if nothing was done.

It was a U-turn from his position two months earlier when he declared there was no circumstance under which he would return to the party's leadership. Rudd said he had changed his mind because of "tens and thousands of ordinary Australians, who have been asking me to do this for a very long time. This has now become urgent. I therefore believe with all my heart that I owe it to offer the people of Australia a viable alternative."

The ballot looked very close until 15 minutes before the vote, when one of Gillard's key backers, Bill Shorten, who had helped her to the top job, changed sides. "The future of this nation and the Labor party is at stake, therefore I shall be supporting Kevin Rudd tonight," he said.

It is unclear what will happen now. The governor general may swear Rudd in as prime minister or could ask him to test his numbers on the floor of the house of parliament with a no-confidence motion on Thursday, the last sitting day before the election on 14 September. It is not certain he would win this vote, as independent MPs who have kept Gillard's minority government in power have not guaranteed their continued support.