The Aberdeen Breviary was printed in 1509-10 and contains services and readings for use in Scottish churches.
Bound at the back is the Compassio Beate Marie, which contains orders of service and readings about the arrival of St Andrew’s relics into Scotland and is the only known copy in existence.
The library said the purchase is a major acquisition that will guarantee public access to an essential piece of Scottish history.
There are three other surviving sets of the Aberdeen Breviary, including one in the National Library’s collections.
National librarian Dr John Scally said: “This is a very significant addition to our collection.
“Each surviving copy of the Aberdeen Breviary makes an important contribution to our understanding of how Scotland’s first books were printed. As is often the case with the first products of a printing press, the work was still somewhat experimental and many corrections were made during the printing process.
“The Aberdeen Breviary is the only work which allows comparisons to be made, shedding new light on Scotland’s first experience with printing.”
The two-volume Breviary had been in the Earl of Strathmore’s library at Glamis Castle in Angus for many years.
It was the only remaining copy in private hands and was offered to the National Library by Christie’s, acting on behalf of the trustees of the Strathmore Estates.
None of the copies are identical and some scholars consider the one from Glamis to be the best surviving example.
The Aberdeen Breviary was the first printed service book to chronicle the lives of the Scottish saints and is thought to have been compiled by the best Scottish historians of the day.
The library said it was the reason for James IV granting a licence for printing to begin in Scotland.
The 16-page Compassio was printed between 1510 and 1532, providing clear evidence that Scots continued to print their own books after producing the Breviary.
Both books are written in Latin and, although partial translations exist, there has never been a full translation or scholarly edition of the text of the Aberdeen Breviary.
The library plans to work with academic partners to create a scholarly edition of the text and a modern translation.
It has already digitised the Breviary and the Compassio which will be made available through its website for everyone to see.
Helen Vincent, the library’s head of rare books and music, said: “We are delighted to have brought these hugely important examples of early printing in Scotland into public ownership where they will be made widely available for study for the first time.”
The Breviary also contains contemporary annotations, including a set of verses and music. It was printed by Walter Chepman, who owned Scotland’s first printing press.
Margaret Ford, Christie’s international head of books and manuscripts science, said: “Christie’s is proud to have facilitated the acquisition of the Aberdeen Breviary and Compassio by the National Library of Scotland through a private negotiated sale, thereby guaranteeing ongoing research and accessibility.”