A police convoy left Greenock Prison, where Megrahi was serving his sentence, just an hour after the announcement of his release was made.
It was greeting by angry jeers from a small group of local residents.
Megrahi was taken to Glasgow Airport where he boarded an Afriqiyah Airways Airbus plane bound for Tripoli, wearing a white track suit and clutching his prison release papers.
The aircraft took off shortly before 1530 BST and arrived in the Libyan capital shortly after 1930 BST.
The government said it had consulted widely before Scottish Justice Secretary Kenny MacAskill made his decision on applications for Megrahi's compassionate release or his transfer to a Libyan jail. He told a media conference on Thursday that he had rejected the application for a prisoner transfer.
However, after taking medical advice it was expected that three months was a "reasonable estimate" of the time Megrahi had left to live.
He ruled out the option of the Libyan being allowed to live in Scotland on security grounds.
The remaining days of my life are being lived under the shadow of the wrongness of my conviction
And Mr MacAskill stressed that he accepted the conviction and sentence which had been handed to Megrahi.
"Mr al-Megrahi did not show his victims any comfort or compassion. They were not allowed to return to the bosom of their families to see out their lives, let alone their dying days. No compassion was shown by him to them," he said.
"But that alone is not a reason for us to deny compassion to him and his family in his final days."
Mr MacAskill continued: "Our justice system demands that judgement be imposed, but compassion be available.
Fiona Trott BBC News correspondent, in Lockerbie
After the Scottish justice secretary made his announcement, life continued as normal in rain-soaked Lockerbie.
People were surprised by the decision he made - the real question was whether they had a strong opinion about it.
When the bombing happened 21 years ago, this town was full of army personnel and the emergency services and local people who helped that night became heroes.
But it happened a generation ago, and, while some American victims' families say his release is incomprehensible, people here in Lockerbie say they don't see things in black and white.
Two families were wiped out here, but the immediate families of the other victims no longer stay in Lockerbie. Some say it's right Megrahi has been released because he is very ill.
Does the decision bring everything to a close? People here say no.
Even though they have put the event in the past, tourists from across the world still come here.
For them, Lockerbie will always be known as the place where PAN AM flight 103 came down.
"For these reasons and these reasons alone, it is my decision that Mr Mr Abdelbaset Ali Mohmed Al-Megrahi, convicted in 2001 for the Lockerbie bombing, now terminally ill with prostate cancer, be released on compassionate grounds and be allowed to return to Libya to die."
Mr MacAskill had been under intense pressure from the US government to keep Megrahi behind bars, with US Secretary of State Hillary Clinton saying his release would be "absolutely wrong".
"Compassion and mercy are about upholding the beliefs the we seek to live by, remaining true to our values as a people - no matter the severity of the provocation or the atrocity perpetrated," he added.
In a statement released after his departure from HMP Greenock, Megrahi continued to protest his innocence.
He said: "The remaining days of my life are being lived under the shadow of the wrongness of my conviction.
"I have been faced with an appalling choice: to risk dying in prison in the hope that my name is cleared posthumously or to return home still carrying the weight of the guilty verdict, which will never now be lifted.
"The choice which I made is a matter of sorrow, disappointment and anger, which I fear I will never overcome."
Reacting to the decision, US president Barack Obama said: "We have been in contact with the Scottish Government, indicating that we objected to this and we thought it was a mistake."
He said they had also contacted the Libyan government to ask that Megrahi not be "welcomed back" but instead placed under house arrest.
"We've also obviously been in contact with the families of the Pan Am victims and indicated to them that we don't think this was appropriate," he added.
The Libyan government has played down claims Megrahi would return to Tripoli a hero.
He will be required to live permanently at a given address in Libya, must agree any change of address and must not travel from Libya without consent, the Scottish Government said.
The families of American victims of the Lockerbie bombing reacted angrily to the news of his release.
Kara Weipz, of Mt Laurel, New Jersey, who lost her 20-year-old brother Richard Monetti, said: "I don't understand how the Scots can show compassion. It is an utter insult and utterly disgusting.
"It is horrible. I don't show compassion for someone who showed no remorse."
New York state resident Paul Halsch, whose 31-year-old wife was killed, said of Mr MacAskill's decision: "I'm totally against it. He murdered 270 people.
"This might sound crude or blunt, but I want him returned from Scotland the same way my wife Lorraine was and that would be in a box."
However, British relatives' spokesman Dr Jim Swire, who lost his daughter Flora in the atrocity, said he believed Megrahi had "nothing to do with" the bombing.
"I don't believe for a moment that this man was involved in the way that he was found to have been involved," he said.
"I feel despondent that the west and Scotland didn't have the guts to allow this man's second appeal to continue because I am convinced had they done so it would have overturned the verdict against him.
"It's a blow to those of us who seek the truth but it is not an ending. I think it is a splitting of the ways."
The BBC's Christian Fraser in Tripoli said that until now, Libyan officials had been careful not to comment in case they jeopardised the release, wary of last minute interventions by the US Secretary of State Hillary Clinton.
Officially there are unlikely to be any triumphant statements, but given the personal involvement of Mr Gaddafi it will no doubt be seen as further evidence of his growing stature on the international stage, our correspondent said.
It is rumoured that he has asked to see Megrahi when he returns, and the timing is perfect - in 12 days' time Libya celebrates the 40th anniversary of the revolution that brought Muammar Gaddafi to power, he added.
Our correspondent said Megrahi's release has been billed by the leader as the new dawn for Libya, and to many it will be viewed as a more palatable ending to one of the darkest chapters in the country's history.
Appeal dropped
Megrahi was convicted of murder in January 2001 at a trial held under Scottish law in the Netherlands.
A first appeal against that verdict was rejected the following year.
However, in 2007 the Scottish Criminal Cases Review Commission granted him a second appeal.
It subsequently emerged he was suffering from terminal cancer but a bid to have him granted bail was refused.
His second appeal got under way this year but shortly afterwards applications were made for both his transfer to a Libyan jail and release on compassionate grounds.
Earlier this week the High Court in Edinburgh allowed Megrahi's application to drop his second appeal.
LOCATIONS WHICH SHAPED MEGRAHI'S TRIAL
1. Tripoli, capital of Libya. Megrahi was born here on 1 April 1952, and worked - according to the FBI - for Libyan intelligence services.
2. Malta. Megrahi's day job, as security chief for Libyan Arab Airlines, took him to their office on the island. From there he would travel to Zurich. The bomb began its journey here in December 1988.
3. Zurich, Switzerland. The bomb's timing device was made and purchased here.
4. Frankfurt, Germany. On arrival, a suitcase later found to have contained the bomb was transferred from an Air Malta jet to a flight bound for London Heathrow.
5. London's Heathrow Airport. Pan Am flight PA 103 took off from Heathrow at 1825 GMT on Wednesday, 21 December 1988.
6. Lockerbie, Scotland. The bomb on Flight PA 103 exploded above the Scottish village at 1902 GMT.
7. Camp Zeist, Netherlands. Here, in a landmark trial, Scottish judges convicted Megrahi in 2001.
8. HMP Barlinnie. Megrahi served the first part of his sentence in this Scottish prison.
9. HMP Greenock. Megrahi was transferred from Barlinnie to Greenock in 2005.
Bookmark with:
What are these?