The prophet Muhammad [PBUH]and his holy Progeny has got the knowledge directly from Allah,as we read in Quran [and ALLAH TAUGHT you everything you did not know]An-Nisa[4]. The knowledge of the Quran was granted by Allah to the prophet when Quran was revealed on the heart of the prophet.
wassalam