CURITIBA, Brazil — Brazilian lawmakers voted on Wednesday to spare President Michel Temer from standing trial on corruption charges, choosing to keep the deeply unpopular leader in place and avoid yet another round of political turmoil.

Mr. Temer needed at least 172 lawmakers to back him or abstain from voting to avoid prosecution. In a marathon congressional session that underscored Brazil’s polarization, 263 deputies supported him and 227 voted against him. The remaining 23 lawmakers abstained or were absent.

Several lawmakers who sided with him said Brazil could not afford more political upheaval. But opponents said that allowing Mr. Temer to stay amounted to an endorsement of the culture of impunity that has made corruption in Brazilian politics pervasive.

Before the vote, shoving matches broke out in the lower house of Congress. Lawmakers critical of Mr. Temer tossed fake bills in the air to denounce what they called the brazenness of corruption in Brazilian politics.