When I first saw this question it reminded me of a chapter I read in Jürgen Osterhammel's The Transformation of the World - A Global History of the Nineteenth Century. He views the museum as a distinctly Nineteenth Century-invention. I'm no expert on Medieval Europe, but since I just saw this post in the Sunday Digest as interesting unanswered question, I'd like to relate what I learned from Osterhammel. I think his book suggests that what the modern museum did not exist in Medieval times, but I would love to see another answer by a specialist of the period to tell us about possible precursors to the modern museum, or to prove him wrong. With that out of the way, this is what I can tell:
The earliest buildings that resemble museums were libraries and archives. Some form of book, scroll or tablet collection buildings have existed for millennia, and were usually archives by modern standards, rather than (public) libraries. Most of these archives were created and maintained by religious institutions to study theology (christian monasteries, buddhist temples) or ruling dynasties to keep national histories. This also meant that records of other cultures, rulers and religions were often destroyed. Osterhammel mentions for example, that China had a long-standing tradition of keeping records, but these were always destroyed once the official history of a dynasty was complete. In short, early archives were state institutions, meant to write histories or resolve legal and theological disputes.
The idea that the general public had an interest in libraries, or a right to obtain knowledge from them is a product of the Enlightenment and later democratic and liberalist ideas. Before these ideas, libraries and archives across the ages and the globe were exclusive to the elite: scholars, administrators, leaders and scientists. As with many things, democratic sentiments made archives and libraries a matter of the general public. The British Museum (1753) was one of the first libraries that was conceived from the outset as a national library, according to Osterhammel. The British set the tone for the establishment of many national libraries, that were usually born from parliamentary or university collections. These institutions were the first collections of knowledge that were meant to be accessible to the public. Keep in mind that, as with voting rights and constitutions, the public often meant the male, white, wealthy public. At any rate, this public library was a European invention. For example, China's first imperial library was founded in 141 BC, but the first public library in China was opened in Changsha in 1905.
That was a lot of talk about books, but generally we think of museums as art collections. The Louvre became the first public art museum after the French Revolution of 1791. The Louvre palace being transformed to a public art gallery is the perfect example of the ideological forces that lead to the modern concept of a museum. During the Nineteenth Century, many princely collections were opened to the public, or officially transferred to new democratic governments. Especially in the United States, the organizational structure of the museum was established: an independent artist, expert critics and curators, all supported by rich benefactors. So, why did this all happen during this specific century?
Art museums were booming in this period, because art itself was fundamentally changed by the politics of the time. Early art was usually meant to tell religious stories, or to depict the grandeur of rulers. These works of art were suited to cathedrals and royal halls, to emphasize the power of the inhabitants of these buildings. In the Nineteenth Century and onwards, romantic, realist and rationalist art was meant to be seen by many eyes. Art became more personal and more political. Art was not just another tool for the old estates, but autonomous, in the words of Johann Joachim Winckelmann. Museums were built in the Nineteenth Century because there was no place for new art in the buildings of the past.
So, this post was a bit longer than I intended, but there's still a lot to add. I'd love to hear additions, thoughts and questions. I think there's a bit more to say on earlier public art displays such as festivals, exhibitions and theatre. Also, you could argue that churches and temples were museums of some kind. Lastly, I am not a medieval expert, and this questions was specifically about medieval museums. Hopefully an expert can elaborate on that period, but I hope that this unanswered question benefitted a bit from my response.
Most of my post was inspired by the first chapter of Jürgen Osterhammel, The Transformation of the World - A Global History of the Nineteenth Century, Oxford UP (2009). I am not a native English speaker, so hopefully my post was readable.