Some patients waited more than a year to be discharged from hospital even though they were clinically ready to leave, figures have shown.
A freedom of information request by the Scottish Liberal Democrats revealed the longest delays across the country between 2013 and 2016.
One patient in Dumfries and Galloway remained in hospital for 508 days.
The Scottish government said bed days associated with delayed discharge dropped in 2016 compared with 2015.
The longest wait was by a patient in Dumfries and Galloway, who stayed in hospital for 508 days in 2013/14.
Delayed discharges of more than a year were also recorded in Fife and Highland health boards.
Waits of more than six months were found in Ayrshire and Arran, Grampian, Greater Glasgow and Clyde, Lanarkshire, Shetland and the Western Isles.
The number of patients waiting for more than three months represented a small minority of those who experienced a delayed discharge during the three-year period.
Several boards, including Forth Valley, Lothian, Orkney and Tayside, were not able to provide the data.
In England, it emerged last month that a hospital in Norfolk applied for a court order to remove a patient who had occupied a bed "unnecessarily" for more than two years.
Delays for health and social care reasons included patients waiting on a care home place, social care support to enable them to live in their own home, and those waiting on a needs assessment to be conducted.
Liberal Democrat health spokesman Alex Cole-Hamilton said: "In November I asked the first minister about a constituent of mine who had spent 150 nights in hospital due to delayed discharge. Nicola Sturgeon described the situation as unacceptable.
"What then are we to make of patients in hospital for up to 500 nights, perhaps because carers can't be found to visit them at home or there isn't a care home place available?"
In September 2013, there were an average of 22,979 beds available compared to 21,815 in September 2016.
Mr Cole-Hamilton added: "Under the SNP, 1,000 beds were lost from Scotland's hospitals during the same three years. Our under-pressure NHS can ill afford delayed discharges on this extreme scale."
A Scottish government spokesman said: "The latest official figures show a 5.1% reduction in bed days associated with delayed discharge in 2016 compared with 2015 - while the same period in England experienced a 23% increase.
"One unnecessary delay, however, is one too many and we have repeated our ambition and expectation that our new integrated health and social care partnerships will address this.
"The draft budget announced an additional £107m to transfer from the NHS to health and social care partnerships to support sustainability in the care sector, bringing the NHS contribution to enhancing social care to around £500m next year, and that funding will be used to further improve social care provisions."