Nicola Sturgeon has offered an unequivocal apology to gay men convicted of sexual offences that are no longer illegal.
The first minister's apology coincided with new legislation that will automatically pardon gay and bisexual men convicted under historical laws.
The bill will also allow the removal of such convictions from criminal records.
Consensual homosexual acts between men aged over 21 in Scotland were only decriminalised in 1981.
And it was not until 20 years later that the age of consent for gay men was lowered to 16.
Speaking in the Scottish Parliament, the first minister said it was "shocking" that consenting sexual activity between men in Scotland was still classed as a criminal activity until so recently.
She added: "Before then, hundreds of people in Scotland were liable to be convicted as criminals, simply for loving another adult."
And the first minister said the simple fact was that parliamentarians in Scotland had, over many decades, supported or at least accepted laws which "we now recognise to have been completely unjust".
Ms Sturgeon said: "Those laws criminalised the act of loving another adult; they deterred people from being honest about their identity to family, friends, neighbours and colleagues; and by sending a message from parliament that homosexuality was wrong, they encouraged rather than deterred homophobia and hate.
"Today as first minister I categorically, unequivocally and wholeheartedly apologise for those laws and for the hurt and the harm that they caused to so many.
"Nothing that this parliament does can erase those injustices, but I do hope this apology, alongside our new legislation, can provide some comfort to those who endured those injustices
"And I hope that it provides evidence of this parliament's determination in so far as we can to address the harm that was done."
The apology was made on the same day as the publication of the Historical Sexual Offences (Pardons and Disregards) Bill.
The legislation will give an automatic pardon to gay men convicted under historical discriminatory laws - but will not apply to behaviour that is still illegal today.
It will also enable men to apply to have convictions for same-sex sexual activity that is now legal removed from central criminal conviction records.
Justice Secretary Michael Matheson said the new legislation was an important step towards addressing the injustice faced by many gay men in the past.
He added: "These discriminatory laws, although abolished, continue to have implications for people to this day and it is only right that we address this historic wrong, which criminalised people simply because of who they loved."
What will the legislation actually do?
The bill would give an automatic formal pardon to people, both living and dead, who were convicted of historical sexual offences where the conviction was for conduct which is now legal.
However, the pardon is a purely symbolic acknowledgement that the laws were discriminatory, and it does not reverse the conviction.
Anyone with a historical conviction for same-sex sexual activity that is now legal would have to apply for a formal "disregard" in order to prevent the information being included in disclosure checks.
It would then be up to Scottish ministers to decide, based on the available evidence about the specific case, whether the disregard is granted.
A successful disregard application means the conviction will not appear on any background check carried out by Disclosure Scotland, and the person will be treated as not having committed the offence.
Derek Ogg, a QC who has campaigned for gay rights for decades, told the BBC's Good Morning Scotland programme that it was a "wonderful day".
He added: "It's Scotland at peace with itself and it is a reconciliation between the people in Scotland who are alive and the families of gay people who are dead, who were prosecuted, convicted, simply because of the gender of the person they loved or fancied.
"You can't underestimate the scars that leaves on people. I've never been convicted of such an offence but the fact is that the law was there and could have been used, I could have been arrested.
"I was at the very beginning of my legal career - my career would have been destroyed. An apology, together with the pardons bill, is appropriate."
Analysis by Brian Taylor, BBC Scotland political editor
As Labour's Kezia Dugdale noted, apologies are rare events in politics and customarily have to be wrung from an unwilling government by the persistence of opposition parties.
This, as she also generously noted, was of a very different character. It was a sincere apology from the FM, delivered with power and empathy.
Furthermore, Nicola Sturgeon was apologising on behalf of a much wider constituency. By definition, she was not apologising for personal or political failings.
Her government had nothing to do with historic laws banning gay sex. Indeed, through such initiatives as gay marriage, their actions have been decidedly in the liberal direction.
But Ms Sturgeon explained that she felt the apology was a proper accompaniment to the bill proposing an automatic pardon with a concomitant right to obtain the disregard of convictions in record books.
His views were echoed by former Scottish Labour leader Kezia Dugdale, who praised Ms Sturgeon for making the apology on behalf of previous governments.
Ms Dugdale told the Holyrood chamber: "This matters because it effects men who are alive today. But it also matters to those who are no longer here.
"We can not risk complacency, and upholding anti-discriminatory laws is critical. Today is a landmark day in Scotland's LGBT history."
Scottish Conservative leader Ruth Davidson added her "unequivocal and wholehearted" apology to that of the first minister.
Ms Davidson said: "My hope for those men and their families is that they now feel a weight lifted and that as well as criminal records formally being wiped clean, any lingering sense of legal stigma, any last shadow of unfair disgrace, is firmly banished."
And Scottish Greens co-convener Patrick harvie said he came out as a teenager when the age of consent was still 21.
Gay and bisexual men convicted of now-abolished sexual offences in England and Wales received posthumous pardons in January, with thousands of living men convicted over consensual same-sex relationships also eligible for the pardon.
It followed the pardoning of World War Two code-breaker Alan Turing for gross indecency in 2013.
Private homosexual acts between men aged over 21 were decriminalised in England and Wales in 1967.