BERLIN — Germany’s highest court on Wednesday overturned a ban on organized medically assisted suicide, allowing terminally and gravely ill patients to seek help ending their lives without leaving the country.

The ruling came after a long-running discussion about the role of doctors and caregivers in end-of-life decisions, one that has special resonance in a country where Nazi doctors killed hundreds of thousands during World War II.

A group of doctors, patients and proponents had sued to change the criminal law banning organized assisted suicide, arguing that the measure infringed on their constitutional right to make decisions about their own lives.

The case centered on wording in the law that forbade professionally assisted suicide and made it punishable by a fine or up to three years in jail. The law allowed assisted suicides for “altruistic motives” but forbade people from offering it to someone else “on business terms.”