How he amassed this great knowledge is revealed in the Anugita chapters of Mahabharata, which states that he got this knowledge by interactions with many learned men, and by his own meditations.
Anugita |