Doctors are crediting public support for helping them strike a new deal on working hours.
After more than a year of negotiations, including strike action, resident doctors and District Health Boards have agreed on a change to working conditions.
The new agreement includes doctors not having to work more than 10 days in a row, and having a total of four days off in a fortnight.
They won't be rostered to work more than four 10-hour night shifts in a row, and will be given recovery time after night shifts.
A new provision will mean doctors could work back-to-back weekends, but to make up for it, rostered days off will be attached to other weekends to allow meaningful time off.
Negotiations started in January 2016, and have officially ended after the changes were ratified by doctors on Friday.
The changes will be effective immediately, with new rosters rolling out over the coming months.
NZ Resident Doctors' Association (NZRDA) president Sara Moeke the public support during their campaign was invaluable.
"We will now have resident doctors working safer hours, which means better care for our patients and safety for the public of New Zealand."
NZRDA national executive member Sam Holford said they were "immensely grateful" to other health workers and members of the public for their support.
"I doubt we could have achieved this outcome without the immense support members of the Association received from so many people both at work and from the communities we serve.
"On behalf of all of us, thank you."
The changes bring an end to working 12 days in a row, or seven night shifts in a row.
Junior doctors went on strike twice during negotiations, walking off the job for 73 hours in January and for 48 hours in October 2016.
The strike in January led to an estimated 6000 cases of elective surgery and outpatient appointments being postponed, so that senior and non-striking resident doctors could focus on emergency and acute care.