Politicians from across the political spectrum voted to overturn the laws that date back to 1900 and left women seeking an abortion in Sydney or elsewhere in the state, as well as their doctors, at risk of prosecution and a prison term of up to 10 years.
The legislation was delayed for months by a small group of conservatives from the ruling Liberal Party who rebelled against their leader.
The passage of the bill was cheered by pro-choice advocates, who had staged several large demonstrations in its support in recent months, countered by similarly passionate pro-life rallies.
Alex Greenwich, the independent representative for Sydney who introduced the bill, said he was sorry it had taken so long for abortion to be removed from the criminal code.
"My deepest gratitude goes to my parliamentary colleagues and to the dedicated women's rights campaigners who have fought toward this for decades," he said in a statement.
Though prosecutions were rare, the procedure was only considered legal if the doctor believed the woman's physical or mental health was in danger.
The new legislation allows abortions to be performed by a doctor on request up to 22 weeks. After that, the consent of two doctors is required.
The law also makes it illegal for an unqualified person to perform an abortion, with the offence punishable by up to seven years in jail.
Amendments made to appease the bill's conservative opponents included tighter regulations on late-term abortions and mandated medical care for any baby born alive following a termination procedure.
New South Wales last year introduced "safe access zones" around medical clinics and hospitals that provide terminations, to stop anti-abortion protesters from harassing women.