MILAN — An Italian appeals court on Friday acquitted former Premier Silvio Berlusconi in a sex-for-hire case, reversing a lower court conviction that had carried a seven-year prison sentence and a lifetime ban on holding political office.

The case revealed details of raunchy, sex-fueled bunga-bunga parties at his private villa attended by a bevy of aspiring showgirls.

The prosecution had alleged that Berlusconi had paid for sex with an underage prostitute and then used his influence to cover it up. He denied the charges.

Acquittal came just as Berlusconi was leaving a facility for Alzheimer’s patients where he is performing a sentence of community service in a separate tax fraud conviction.

Berlusconi thrust his hand from a car as he was being driven away from facility and a political supporter grabbed his hand and shook it. The window was then rolled up and the media mogul waved as he was driven away.

Prosecutors can challenge Friday’s acquittal in Italy’s highest criminal court, the Court of Cassation.

The 77-year-old three-time former premier remains influential on the Italian political scene as head of his Forza Italia party, despite the tax fraud conviction’s ban on holding political office for two years that led the Senate to yank his seat.