On the edge of St Andrews, in a building not unlike an Ikea warehouse, lies one of the great treasures of Scotland.

So bland as to be almost invisible, sitting between the chemistry buildings and medical school, this prefab-style shed houses the university's rare books collection. It's a temporary home, a makeshift arrangement to bridge the collection's move from the bowels of the main library to a custom-built library, but once over the high-security door, the look of the place is irrelevant. In an ancient town filled with history and fascinating things to see, nothing can compete with the contents of this place.

There was a time when the university's rare books were barely talked about. When I was a student, none of us knew that, while we were working away in the library on dull modern texts, row upon row of books from the middle ages were stored several floors below our feet.

Daryl Green, one of the University of St Andrews' rare books librarians, says that until the mid-1980s the collection was "pretty much kept under lock and key". Now, however, the university is not only happy for people to know about it, but it actively promotes the collection. To this end, to mark the university's 600th anniversary, it has published a glossy six-part, large-format boxed work showcasing some of the collection's finest gems. Taking one book from each decade since 1413, and dividing the collection by themes - divinity and politics, the arts, the natural world, geography and exploration, language and literature, and astronomy and mathematics - it offers an extraordinary keyhole not only on the books St Andrews has acquired over the centuries, and the development of the printed book, but on the evolution of knowledge, and of Scotland's role in the world's intellectual advances.

Green, who wrote the anniversary box set, could not be happier to show someone around the stacks. Brought up in St Louis and Ohio, he took a degree at the University of Ohio, did an MLitt in Medieval Studies at the University of York (he then worked in the York Minster library), and trained as a librarian at the University of Illinois. He came to St Andrews in 2010 as a rare books cataloguer, and then to his current post.

As he bounds between the stacks, pulling out illuminated 15th-century texts, or a well-thumbed first edition of Adam Smith's The Wealth Of Nations, or a gold-leafed medieval book accidentally sold by the Bavarian State Library during the Second World War, he looks like a man in seventh heaven, as well he might. If he did not have a wife and child to go home to each night, one suspects he might opt to stay locked in here.

Showing me around the collection, he starts with a couple of books kept in a safe alongside old pieces of equipment from the university's physics and chemistry departments, which are now part of the university's museum collection.

One is the only book Leonardo da Vinci ever illustrated, Pacioli's Divina Proportione (1503-12). The Renaissance genius's distinctive style is evident in his mathematically elegant fonts and diagrams, illustrating various geometric shapes and letters.

The other is the world's first work illustrated by photographs, William Fox Talbot's series of magazines called The Pencil Of Nature ­(1843-52), in which he used photographs of Lacock Abbey among others to demonstrate his revolutionary technique. This method trounced Daguerre's innovation, and ushered readers into a wholly new era. Fife having proved itself a cradle of photographic experimentation, St Andrews University has an astonishingly good photographic collection, comprising around 800,000 items, to which it continues to add.

Numbers and statistics are dull, but there is nothing boring about the quantity of rare books held here. Green reckons there are 210,000 volumes in the historic collection, "which is huge for a university so small". When asked what one can tell about St Andrews from its library, he replies: "Of all the libraries I've been in, it says to me how old the knowledge base is here at the university: how old, and how revered, and how well backed by the textural witnesses they had."

Where most universities in the UK, outside the ancient institutions, have collections bought in the past 100-150 years, he says, "a lot of the books we have in the collections have been here since the 15th century, the 16th century. This is pre-newsprint, pre-internet. This is the way ideas circulated. You had so many scholars here who had such a huge effect on European religious and political circles, the ideas not only circulated here but went forth from here as well."

To that extent, he agrees that Scotland punched above its weight. "St Andrews' and Scotland's effect on the intellectual circles of the 15th and 16th centuries especially were much greater [than you'd expect]. As someone coming not from Scotland, it's really surprising."

One of the books he brings out - the first printed anywhere in Scotland outside Edinburgh - is a Catechism in Scots, produced a couple of years before the Reformation, in 1552, perhaps in a desperate bid to ward off the impending religious revolution. The fact it even survived in such a hotbed of radicalism as St Andrews is remarkable. Also interesting are the signs of the amateurish production, the inking uneven, the pages cramped. A few years after this Catechism was published, the printer was run out of town for his Catholic sympathies.

Over the space of two hours and more, Green takes me through the collection. It is like letting a child loose in a chocolate factory. There's the grisly anatomy book used by the first professors of medicine in the early to mid 18th century, who did not have cadavers on which to teach students. Instead they used Vesalius's Anatomy, De Humani Corporis Fabrica (1563-72) and pasted in updated versions of his original diagrams, no doubt in the hope this would improve surgical accuracy.

There's a first edition of Copernicus's heretical De Revolutionibus Orbium Caelestium (1543-52), containing the first-ever depiction of the sun being at the centre of the universe, and the Earth circulating around it. That page bears red wax marks, showing it had been covered up, "so if a censor came by the library and looked through it, he would pass that". And there's the exquisite Wardlaw Bible, a conventional if richly produced family bible, whose elaborate embroidered covering brings the 17th century back to life.

But there is more to this inspiring collection than the value of the texts themselves. In book after book, one finds notes written in the margins by readers. On one, someone has done rough sums, perhaps before heading out to the market. In others, there are sketches of pointed fingers, to guide them to particularly juicy parts of the text. One such picture, in a book owned by the Archbishop of St Andrews, depicts a finger as long as ET's, and a sleeve drawn with an attention to detail Arthur Rackham would have admired. Instantly, you are transported back to the middle ages, to the company of the reader who pored over this book.

Asked to name his favourite title, Green produces a copy of Boccaccio's Illustrious Men, translated by John Lydgate and printed in 1555. It is not the book itself that is remarkable, however. "What really bowled me over," says Green, "is that there are two elements of provenance which really make this thing stand up and shout. First is a bit of two-line verse, in English, signed by Arthur Poole, a 16th-century Londoner. He has also written how much he's paid for the book - 14 shillings and sixpence, which is quite interesting."

The other is far more unsettling. As he was cataloguing the book, Green turned to the blank pages at the back. There he discovered, in a woman's handwriting, something titled: "For a pomander against the plague". A pomander, Green explains, "was a stinky, unctuous thing you wore round your neck to ward off the plague".

This page was inscribed during an outbreak of the pestilence in London, and what follows is a recipe, as neat as anything Mrs Beeton would have devised. It lists the ingredients, such as ambergris, cloves and cinnamon, the weight to use, and how much it would cost, followed by a detailed description of how to prepare and mix the various elements.

"It really is electrifying," says Green, "such a personal window into the history of London." The unanswered question hangs over the book: did the writer survive the plague?

That is but one of the tantalising insights into the past that the university's remarkable collection holds. By the time I leave, my head is reeling, my imagination in overdrive. Those readers envying me my tour - and who would not? - will be pleased to learn that, while guided tours are rarely on offer, with only a very few exceptions all titles in the rare books collection are available for the public to read.

600 Years Of Book Collecting is ­published in a limited edition boxed set, price £60, available to order from the University of St Andrews website's online shop, http://onlineshop.st-andrews.ac.uk/browse/extra_info.asp?compid=1&modid=1&catid=165&prodid=3427