Even as both sides acknowledged concessions, Tuesday’s vote was an important first step for the unlikely coalition of liberals and conservatives — including the American Civil Liberties Union, the American Conservative Union, Koch brothers and the liberal Center for American Progress — who locked arms in recent years and pushed lawmakers to reconsider the way the federal government administers justice three decades after the war on crime peaked. In one of this Congress’s final acts, every Democrat and all but 12 Republicans voted in favor of the legislation — an outcome that looked highly unlikely this month amid skepticism from Republican leaders.

For Republicans preparing to relinquish total control of Washington next month, the bill’s passage offered one final victory on their own terms and handed Mr. Trump a bipartisan policy achievement that he can tout as he seeks re-election. Liberals saw reason to celebrate, as well, even as they called for more aggressive changes: In gaining the support of Mr. Trump and so many Senate Republicans, they believe they have shifted the terms of policy debates around criminal justice in a way that could set the stage for additional changes on the federal level and in the states.

“This bill in its entirety has been endorsed by the political spectrum of America,” said Senator Richard J. Durbin, Democrat of Illinois, who has led the push for changes along with two Republicans, Senators Charles E. Grassley of Iowa and Mike Lee of Utah. “I can’t remember any bill that has this kind of support, left and right, liberal and conservative, Democrat and Republican.”

Mr. Trump quickly touted the vote on Twitter, saying that the changes would “keep our communities safer, and provide hope and a second chance, to those who earn it.”