A government bid to change the rules on electing Commons speakers - seen by Labour as an attempt to oust John Bercow - has been defeated.
Mr Bercow was almost overcome by emotion as he announced the result of the vote - 228 to 202.
He had earlier told MPs: "I'm not going anywhere."
William Hague wanted a secret ballot to decide the Speaker's future after the election but Labour and some Tory MPs said it was a "grubby" plot.
They accused the Commons leader of springing the motion on MPs in the final hours of the current Parliament, before it breaks up for the general election.
Among the No votes were 23 Conservative MPs and 10 Liberal Democrats.
In extraordinary scenes, MPs hurled abuse at Mr Hague for allegedly conspiring with Prime Minister David Cameron to oust Mr Bercow, who is unpopular with much of the Tory leadership.
Moving the motion, Mr Hague, who is standing down as an MP, said there was a long tradition of secret ballots in Britain to decide powerful positions.
He said: "I think a secret ballot frees members of this House completely from pressure from their parties or from the chair.
"I think that is the right thing in principle to do."
In a tearful speech, Tory MP Charles Walker, chairman of the Commons procedure committee, claimed he had been "played for a fool" by ministers over the issue.
Mr Walker said he had attended Mr Hague's leaving drinks this week, spending 20 minutes saying goodbye to his special adviser and speaking to Deputy Commons Leader Tom Brake and Chief Whip Michael Gove, "all of whom would have been aware of what was going on".
He said: "I have been played as a fool and when I go home tonight I will look in the mirror and see an honourable fool looking back at me and I would much rather be an honourable fool in this and any other matter than a clever man."
Labour MPs got to their feet and gave the Tory MP a round of applause - something that is not supposed to happen under Commons rules.
Former Tory minister Greg Barker sparked uproar when he questioned Mr Bercow's suitability to chair the debate.
Mr Bercow replied: "It is commonplace for the Speaker to be in the Speaker's chair. I'm genuinely sorry if that disquiets you."
Asked earlier by a Labour MP if he would chair the debate Mr Bercow had said, to cheers: "I am in the Chair, and I am intending to remain in the Chair, today and, I hope, subsequent to today."
He assured MPs he would chair the debate with "competence and fairness," before adding with a smile: "I am not going anywhere."
Veteran Labour MP Sir Gerald Kaufman accused William Hague of a "grubby, squalid and nauseous" attempt to change the rules on Speaker elections.
"Sad, sad, sad Mr Hague, change your mind," he shouted at the Commons leader.
A smiling Mr Hague said he had received worse "personal abuse" in his time in the Commons and the Labour MP's words would be "water off the back of this particular duck". Mr Hague said he made no apology for allowing a debate on the Speaker election rules, which the public wanted.
Labour's shadow commons leader Angela Eagle said Mr Hague should be "ashamed of himself" for "going along" with what she claimed was a plot by David Cameron to get rid of Mr Bercow.
"It is a petty and spiteful act because he hates his government being properly scrutinised thanks to this reforming Speaker," she told MPs.
In a statement, she added: "This is a humiliating defeat for David Cameron on the last day of this Parliament. Instead of talking about ways to improve the lives of working people, in the last week all the prime minister has done is play petty partisan games and arrogantly talk about his retirement plans. In today's vote decency and democracy prevailed."
Under current rules, if a Speaker's re-appointment is objected to, a vote is held, with MPs filing through the division lobbies outside the chamber to cast their vote.
This means it is clear who does or does not support the incumbent.
Mr Bercow's supporters claimed the government's attempt to move to a secret ballot was motivated by a desire to make it easier to remove him from the Speaker's chair.
At the very end of the day's proceedings at Westminster, Mr Hague and Mr Bercow exchanged words as they shook hands after the traditional prorogation ceremony to close Parliament.