The vote in the lower house threw out the charges just an hour after Temer, 77, was discharged from hospital where he was admitted earlier with a blocked urinary tract yesterday.
Temer's allies had been widely expected to bar a demand by prosecutors for the president to face trial in the Supreme Court, but a note of drama was injected by the surprise news that he had been taken to hospital.
On release, he was told to go home to rest, the presidency said.
The first president in the country to face criminal charges while in office, Temer is accused of obstruction of justice and racketeering. He denies any wrongdoing.
More From This Section
The lower house of Congress would have had to vote by a two-thirds majority to force Temer to stand down for 180 days and the Supreme Court to open a trial. However allies rapidly racked up more than a third of the votes, blocking the process.
In August, allies in Congress threw out an earlier bribery charge by a heavy margin.
Ironically, what made it easier for Temer was that scores of those judging him in the lower house are themselves targets of anti-corruption probes.
Many deputies want to slow down Brazil's energetic anti- corruption prosecutors who have unleashed the historic "Car Wash" probe against the country's elite over the last three years.
Among the sweeteners handed out by Temer were removing Sao Paulo's Congonhas airport from a list of big privatisations and lowering environmental protection fines -- a gesture to the powerful agricultural industry lobby.
Temer had "more than sufficient votes to turn the page, even with an erosion of his political capital," a government source who asked not to be identified told AFP.
Opposition deputies acknowledged they did not have the numbers to bring down Temer, despite his huge unpopularity with Brazilians. They were reduced to trying to string out the debate by preventing a quorum and slowing down the vote.
Market watchers say the most significant result of Wednesday's vote will be indications of how many deputies support the president's bid to cut the generous pension system.
Temer says the reforms are needed to tame a runaway budget, but the proposals are hugely unpopular with Brazilians and Congress has been cagey about whether to support the policies ahead of general elections in 2018.
After taking over the presidency in controversial circumstances following leftist president Dilma Rousseff's impeachment last year, Temer has never had any illusions about his public standing, and he is not going to run for re- election.