Auction house Lyon and Turnbull said there was international interest in the document, signed with a message in French, as it went under the hammer live and online on Wednesday.
The letter is an appeal from Mary Queen of Scots to the French ambassador in England to allow the safe passage of Scottish nobleman, George Douglas, to France.
It was written in Carlisle Castle two months after her escape from Lochleven Castle in Perthshire on May 2 1568, where she had been imprisoned for nearly a year following a forced abdication in favour of her infant son, James VI.
Mary hoped that by helping to ensure a safe journey to France for George Douglas, he would intercede with the French king on her behalf to help secure her freedom.
Cathy Marsden, rare books, manuscripts and maps specialist at Lyon & Turnbull, which has its headquarters in Edinburgh, said: "There was international interest in this letter, the sum achieved at auction reflecting the enduring fascination with one of the best known monarchs in history.
"There are 12 lines written by a secretary, and an additional six lines in Mary's own hand. The letter is signed, 'Votre bien bonne amye, Marie,' which translates as 'Your very good friend, Mary.'
"A letter bearing her personal mark is rare and we're delighted to have been involved in the sale of such an important text."
The letter sold for more than double the asking price of £14,000.
The document asks the French ambassador to Queen Elizabeth I to lend George Douglas, the bearer, 300 ecus (gold coins) and to negotiate with the French royal family to secure George's trouble-free passage.
Mary's escape from Lochleven had been helped by George Douglas and his cousin, William Douglas.
She was apprehended by Richard Lowther, deputy governor of Cumberland, and escorted to Carlisle Castle.
Mary was imprisoned by Queen Elizabeth I for 19 years before she was beheaded in Fotheringhay Castle in Northamptonshire on February 8 1587 at the age of 44.
The price includes buyer's premium.