The Voluntary Assisted Dying bill has passed the Victorian Upper House 22 votes to 18, after 24 hours of heated debate.
The bill has been thoroughly debated and widely amended since it was introduced by the Labor government.
The outcome follows a second night-long sitting of the legislative council and amendments must now be approved by the Lower House but are not expected to be opposed.
"This is a momentous day in the parliament of Victoria," Labor MP Gavin Jennings told the legislative council after the narrow vote.
If successful in the Lower House, Victoria will become the first state to legalise assisted dying for people with a terminal illness.
The scheme will be restricted to residents of Victoria who have lived there for at least one year.
Dying with Dignity Victoria has welcomed the decision.
“DWDV has advocated for these laws since its inception in 1974 and it is extremely gratifying to see the hard work done over the years finally resulting inhumane, safe and compassionate legislation,” DWDV President Lesley Vick said.
“This reform will also be welcomed by the overwhelming majority of Victorians who support VAD and we commend the Victorian members of parliament who recognised this support in the community and passed the VAD Bill.”
Emotions were raw when the vote was finalised.
"You've made a terrible mistake," anti-voluntary assisted dying campaigner Frances Beaumont yelled at MPs.
Euthanasia advocate and media identity Andrew Denton left parliament after the vote without comment, saying he would leave the commentary to the politicians.
Earlier on Wednesday, exhausted politicians cheered after arguing their way more than 100 of the bill's 141 clauses and amendments.
"The fatigue, the sleep deprivation, does cloud the judgement and one's alertness," Liberal MP and assisted-dying opponent Inga Peulich told the Legislative Council.
The bill would give terminally-ill Victorians in intolerable pain and with less than six months to live the right to ask to end their lives using prescription medication.
MPs on Wednesday teased out concerns over the security of the locked box in which the lethal medication would be delivered.
They also debated how to best protect patients from abuse and coercion, and whether assisted dying would be listed as the cause on a death certificate.
Special Minister of State Gavin Jennings earlier criticised opponents of the bill for delaying debate.
"I have spent hours answering repetitious questions, listening to repetitious contributions, speeches that cover a lot of ground that we have actually covered," he said.