The Kirk voted through the change at its six-day General Assembly with 339 versus 215 against.
The vote comes after Assembly members voted last year to recognise clergy in civil partnerships.
It means ministers already in same-sex marriages can continue to serve and also that ministers may now marry members of the same sex.
Church votes in favour of recognising Ministers and Deacons in same sex marriages by 339 for to 215 against #GA2016 pic.twitter.com/6DrtazkXyX
— Church of Scotland (@churchscotland) May 21, 2016
Congregations who do not want ministers in gay marriages do not have to appoint them if they don't want to.
Ministers will also not be able to perform gay marriages, however the General Assembly has commissioned a report exploring whether the Church of Scotland should perform them.
The Church of Scotland has said it "maintains its traditional view of marriage between a man and woman".
The Church of England has also said this, but it does not allow its clergy to be in same-sex marriages or perform them.