The bill passed just shy of a year since it was introduced to Parliament as part of the Government's100-day plan improve access to medicinal cannabis for terminally ill people and those in chronic pain.
Last month, during the bill's second reading, Health Minister David Clark made changes to the bill that expanded the defence to all people needing palliative relief, rather than just those with a year or less to live, as it previously was.
The changes also created a requirement for regulations for the Medicinal Cannabis Scheme to be in place no later than one year after the law comes into effect, allaying concerns that it could take as long as 2020 before the regulatory framework was in place.
It made clear that cannabis varieties already in New Zealand could be used for medicinal products, prompting at least one therapeutic cannabis company to call for illicit growers to come forward with their unique strains.
National supported the Government's bill at first reading but then pulled its support in July with its own bill which it said set out a more comprehensive and well-researched regime for the use of medicinal cannabis.
It then supported the bill at second reading, with a condition that loose-leaf cannabis not be included, and that aspects of its own bill were included.
National's associate health spokesman Shane Reti's bill contained more detail around regulating the regime but Clark said Reti's proposals had been considered and rejected on the advice of health officials and clinicians.
Following the bill's passing, Clark said the statutory defence would be available to around 25,000 people who could benefit from palliative care.
"This is compassionate and caring legislation that will make a real difference to people living in pain and nearing the end of their lives," Clark said in a statement.
"Today's vote in Parliament clears the way for the creation of a Medicinal Cannabis Scheme that will allow New Zealand companies to manufacture medicinal cannabis products for both the local and international market."
Those medicinal products will be available on prescription.
Reti said the Government had decriminalised marijuana by stealth.
"The Government's Misuse of Drugs (Medicinal Cannabis) Amendment Bill which has passed today allows the smoking of loose-leaf cannabis in public and does not provide the details or framework for a permanent medicinal cannabis scheme.
"We support medicinal cannabis but strongly oppose the smoking of loose leaf cannabis in public. Smoked loose leaf is not a medicine," Reti said.
Green Party drug law reform spokeswoman Chloe Swarbrick said the bill provided certainty and clarity to patients and their whānau.
"Too many New Zealanders are going into debt to access expensive big pharma products, or being turned into criminals in having to access an unregulated black market for cannabis," Swarbrick said in a statement.
"Today we move towards delivering on something the Greens have worked on for decades. Today is for champions and advocates like Helen Kelly, Rose Renton, Rebecca Reider, Nandor Tanczos and so many more bold and brave New Zealanders," she said.