A bill to decriminalise abortion will head back to the NSW lower house for MPs to consider amendments approved by the upper house before it becomes law.
An amended bill to decriminalise abortion will return to the NSW Legislative Assembly where it faces its final hurdle before it becomes law.
The bill passed the NSW upper house 26 votes to 14 on Wednesday night after nearly 40 hours of discussion - making it the third-longest debate in the state's upper house.
The amended private member's bill will return to the lower house on Thursday morning before it becomes law.
The bill, presented to parliament in August by Independent MP Alex Greenwich, takes abortion out of the criminal code and allows terminations up to 22 weeks as well as later abortions if two doctors agree.
Upper house MPs on Wednesday night passed an amendment that recognised doctors performing abortions after 22 weeks can seek advice from a multi-disciplinary team or hospital advisory committee.
Labor MP Penny Sharpe, who is one of 15 co-sponsors of the bill, on Wednesday night said the vote was 119 years in the making.
The draft legislation has been opposed by religious groups, anti-abortion activists and several MPs who raised concerns about late-term and sex-selective abortions, conscientious objection and the way the bill was introduced.
Tensions reached a climax last week when Liberal MPs Tanya Davies, Mathew Mason-Cox and Lou Amato said they would move a leadership spill motion against Premier Gladys Berejiklian over her handling of the bill.
The rebel MPs, who ultimately withdrew the motion, said it had been made clear that "at an absolute minimum" four key amendments were required to ensure continued Liberal Party membership.
Mr Mason-Cox said there has been "significant improvement" to the bill through amendments but declared it "dangerous".
Amendments passed in the upper house include one clarifying that doctors must provide appropriate care to babies born alive after a termination.
Another states the NSW parliament opposes sex-selective abortions, and a future report on the issue is to include prevention recommendations.