BERLIN — In what the justice minister, Heiko Maas, called a “belated act of justice,” the German Parliament has voted unanimously to void the convictions of roughly 50,000 men prosecuted for homosexual acts since World War II.

The measure, approved on Thursday, also awards compensation to about 5,000 of the men who are still living. Each will receive 3,000 euros ($3,350), and an additional €1,500 for each year spent in prison for the convictions.

Sex among men was illegal in Germany from its founding in 1871 until the last years of the 20th century, under Paragraph 175 of the German Criminal Code. The provision was modified a number of times over the years, and was sharpened in the Nazi era, when it was much more strictly enforced. Unlike many other Nazi practices, the strict ban was retained by the West German authorities for decades.

“This has been a very, very long fight for the rehabilitation of gay men who were convicted in this democratic German state — not in the National Socialist state, but in the democratic German state,” said Axel Hochrein, a board member of the Lesbian and Gay Federation in Germany.