Ecuador has become the second country in Latin America after Colombia to decriminalise euthanasia.
Its constitutional court voted seven to two in favour of allowing doctors to help a patient die.
The court said the crime of homicide would no longer apply to clinicians working to preserve the right to a dignified life.
The lawsuit was brought by a woman suffering from a neurological disease known as ALS.
She had told the court in November that she was experiencing pain, loneliness and cruelty, and wanted to rest in peace.
The Roman Catholic Church, to which most Ecuadoreans belong, remains staunchly opposed to euthanasia.
Colombia decriminalised euthanasia in 1997.
ALS sufferer Paola Roldán, who is confined to her bed, brought her lawsuit in August, AFP news agency reports.
Amyotrophic lateral sclerosis (ALS) is the most common form of motor neurone disease, a rare condition that progressively damages parts of the nervous system, leading to muscle weakness, often with visible wasting.
Ms Roldán contested an article of the penal code which made euthanasia a crime of homicide carrying a sentence of between 10 and 13 years in prison, AFP says.
“I want to rest in peace,” she told a court hearing in November via video link. “What I experience is painful, lonely and cruel.”
The court ruled that “it would be unreasonable to impose an obligation to stay alive on someone who is going through this situation”.
“Every human being can make free and informed decisions when their personal development is affected which… includes the option of ending the intense suffering caused by a serious and irreversible bodily injury or a serious and incurable illness,” it said.
After the ruling, Ms Roldán told reporters that her country had become a “little more welcoming, freer and more dignified”.
“The fight for human rights is never a paved road,” she added.
Her father Francisco Roldán told reporters he had mixed feelings because while Paola had been able to achieve a “historic event” and a “legacy for Ecuadorian society” his family had a “half-broken heart because there could be an outcome… which is the death of my daughter”.
“We are supporting Paola,” he said.
A bill on euthanasia will now have to be drafted and approved by Congress, a process that could take many months, but Ms Roldán’s lawyer Farith Simon argued that the ruling was “immediately enforceable”.