The Church of Scotland's ruling General Assembly has voted to allow actively gay men and women to become ministers.
Assembly commissioners in Edinburgh voted in favour of a proposal that allows liberal parishes to opt out of the church's policy on homosexuality.
Legislation will be drafted for next year, then go to regional presbyteries and return for final approval in 2015.
Divisions were caused when the first openly homosexual minister was appointed by the Kirk four years ago.
Two congregations and six ministers broke away.
The vote to allow gay ministers in civil partnerships follows a report by the church's theological commission, which set out arguments on both sides.
General Assembly rules dictate it must be approved at a presbytery level. Draft legislation will be presented at next year's assembly, go out to regional presbyteries and then get official approval at the 2015 gathering.
The Moderator of the General Assembly of the Church of Scotland, the Right Reverend Lorna Hood, said: "This is a massive vote for the peace and unity of the Church."
The Kirk said that after a "full but gracious debate" it affirmed its current doctrine and practice in relation to human sexuality but moved to permit sessions wishing to depart from the traditional position to do so.
Mrs Hood added: "This was a major breakthrough for the Church but we are conscious that some people remain pained, anxious, worried and hurt. We continue to pray for the peace and unity of the Church."
The dilemma faced by the Church of Scotland goes back to 2009, when the openly gay minister Scott Rennie was appointed to the Queen's Cross parish in Aberdeen.
He was backed by most of his congregation and by the General Assembly, but the decision resulted in protest and the break-away of a small number of congregations and ministers.
Speaking to BBC Radio Scotland, Reverend Rennie said the General Assembly vote was a "good decision" and a "fair compromise".
Tom French, from the Equality Network, welcomed the move by the Kirk.
He said: "This is a positive step forward for a more equal society, and speaks to the progressive values of 21st Century Scotland."
A Free Church of Scotland spokesman said: "We don't understand what's going on in the Church of Scotland, and suspect the vast majority of the Scottish public don't have a Scooby either.
"We believe that Scotland needs the guidance of the national church rooted in the teachings of the Bible, irrespective of public opinion and pressure to conform."
At the Kirk's gathering in 2011, commissioners voted to accept gay and lesbian clergy - on the condition they had declared their sexuality and were ordained before 2009.
At that General Assembly, the theological commission was set up to report this year, before a final decision was taken.
Last June, a Glasgow Kirk became the first to split from the Church of Scotland over the issue of gay clergy. St George's Tron Church said its 500-strong congregation believed the General Assembly's decision in 2011 had "marginalised the Bible".
And in February, the Rev Dominic Smart and his congregation of 300 at Gilcomston South in Aberdeen left the Church over the same issue.
A total of six ministers have now left the church over gay ministers.
The General Assembly is the supreme court of the Church of Scotland and its annual national business meeting.
The Assembly was first held in 1560, the year of the Scottish Reformation which marked the beginning of the Protestant Church in Scotland.
About 850 church commissioners - mainly ministers and elders - from across the church's 48 presbyteries, gather in the Assembly Hall on the Mound in Edinburgh for a week in May.