This question was posted so long ago, but I will post an answer because you might still be looking for it.
First of all, I would like to draw you attention to the fact that if all that is being said about Mezzofanti is true, he probably was a very gifted student; just like you could consider Mozart to be a genius in music. On the Wikipedia page there's a brief explanation of how he came into contact with many different langauges, i.e. by talking to visiting missionaries, which supposedly happened to be around when he was very young. Below is the link to the page.
https://en.wikipedia.org/wiki/Giuseppe_Caspar_Mezzofanti#Life
Still, many people travel to different countries today but almost nobody could be said to have learnt 10+ languages in the span of a few years by just talking to people. Most of us would have trouble picking up new languages even after living in a foregin country for a while. That leads me to hypothesise that either he was a natural talent or the information about him is false.
At any rate there are a few points we should look at:
The language listed as well known are 30 and of these some are labeled as 'Ancient ___', 'Modern ____'; even though knowledge of 'Ancient Greek', for example, is not equivalent to 'knowing Modern Greek', I am sure it helps.
He was educated by the Church in the 18th century. I want to emphasise this because many children at that time fluently spoke and wrote both ancient Latin and Greek which are 'dead' langauges, and as such not easy to master. Plus having studied Latin myself I can say that it is a very difficult langauge, no wonder even in Roman times it wasn't widely spoken. At any rate, these two languages form a very good basis for any future langauge learning, if studied in depth. Latin helps with learning romance langauges and even with some others like German, just because it trains you to understand and work with a very complex grammar (much more than English). And ancient Greek just adds a bonus for a similar reason.
He might just have had a lot of free time and a very good memory and flexibility in adapting to different languages. Also, after learning the first few languages it should come easier to learn more.
Some people who live in areas where several languages are spoken (like in India) tend to merge different languages' grammars and vocabulary attaining an ability to speek all of these languages even though they make mistakes or sound non-native. This might have been the case for some of the languages Mezzofanti learnt (maybe for the Native American dialects or many of the European langauges)
Moreover, there is a difference between using a language to communicate and Mastering a langauge. Although, since he was appointed professor to teach some of those languages I would assume he knew them thoroughly.
As an aside, I would like to mention that since ancient times there is been around somethig called "Art of Memory" which includes techniques deemed to train your memeory. Maybe Mezzofanti was aware of some of these and practiced them (Dominician monks are known to have used mnemonic techniques since centuries ago). It is likely he could be exploiting some association techinques to remember words in different languages, but I tend to believe he had a natural inclination to deduce and interpret uknown languages by using the ones he had perfectly mastered already.
Summing up it seems to be due to his very good memory and no other things in particular (although I belive exposition to Latin and Greek at a young age played a role in this as well). It is also quite astonishing to hear that he mastered Chinese in 4 months and that he found Chinese to be one of the most dificult languages he ever tried to learn.