As per Mahabharata, Krishna did severe penance for several months. Lord shiva,who was pleased with the devotion of krishna ,and asked him to choose boons. Parvati devi,the consort of Lord Shiva, who was pleased with the devotion of Krishna towards Lord Shiva, also gave some boons. Sri Krishna was a "Brahma Vit"(The Knower of Brahman). Even in his past life as Narayana Maharshi, he did penance in Badari forest along with his companion Nara Maharshi (who incarnated as Arjuna). He never bothered about material things. He knew that Lord Shiva was the 'Self' of all beings. So whenever he worshiped Lord Shiva, he worshiped,verily,as his own 'Self'. But, For the purpose of Leela, he took Avatara as Krishna. In that Avatara, Krishna showed his Leela by being 'Brahma vit' in spite of all opulences. Even while asking for some material boons, Krishna never asked for the sake of his personal enjoyment. He asked material boons for the sake of others who depend on him viz., wives,parents,citizens..etc. or to show Leela. Even while displaying Avatara Leela,he never had even a bit of "Ego". He completely identified himself with "Shiva consciousness". The body of Krishna was just an instrument driven by All-pervading Shiva. Krisha was completely merged with All-pervading Shiva. Those who read Bhagavad Gita can understand this.
When Krishna performed Pashupatha Deeksha successfully, the all-pervaing Nirguna Shiva appeared as Saguna-Brahman along with Shakti.
Mahabharata, Anushasan parva Book 13, chapter 15 & 16 explains the event. Here are some of the relevant verses.
sa vipra saṃghāś ca surāsurāś ca; nāgāḥ piśācāḥ pitaro vayāṃsi
rakṣogaṇā būta gaṇāś ca sarve; maharṣayaś caiva tathā praṇemuḥ
The innumerable Brahmanas there present, the deities and the Asuras, the Nagas, the Pisachas, the Pitris, the birds, diverse Rakshasas, diverse classes of ghosts and spirits, and all the great Rishis, then bowed down unto that great Deity.
mama mūrdhni ca divyānāṃ kusumānāṃ sugandinām
rāśayo nipatanti sma vāyuś ca susukho vavau
There then fell upon my head showers of celestial flowers possessed of great fragrance, and delicious winds blew on the spot.
nirīkṣya bhagavān devīm umāṃ māṃ ca jagad dhitaḥ
śatakratuṃ cābhivīkṣya svayaṃ mām āha śaṃkaraḥ
The puissant Sankara then, devoted to the good of the universe, looked at the goddess Uma and the lord of the celestials and myself also, and thus spoke unto me,
vidmaḥ kṛṣṇa parāṃ bhaktim asmāsu tava śatruhan
kriyatām ātmanaḥ śreyaḥ prītir hi paramā tvayi
vṛṇīṣvāṣṭau varān kṛṣṇa dātāsmi tava sattama
brūhi yādava śārdūlayān icchasi sudurlabhān
-We know, O Krishna, that thou, O slayer of foes, art filled with the greatest devotion towards us. Do what is for thy good. My love and affection for thee is very great. Do thou ask for eight boons. I shall verily give them unto thee, O Krishna, O best of all persons, tell me what they are, O chief of the Yadavas. Name what thou wishest. However difficult of attainment they be, thou shalt have them still.'"
kṛsna said : ( Mahabharata, Anushasan parva Book 13, chapter 16, Verses 1 to 11 )
mūrdhnā nipatyaniyatas tejaḥ saṃnicaye tataḥ
paramaṃ harṣam āgamya bhagavantam athābruvam
dharme dṛḍhatvaṃ yudhi śatrughātaṃ; yaśas tathāgryaṃ paramaṃ balaṃ ca
yogapriyatvaṃ tava saṃnikarṣaṃ; vṛṇe sutānāṃ ca śataṃ śatāni
evam astv iti tad vākyaṃ mayoktaḥ prāha śaṃkaraḥ
The blessed Krishna said, 'Bowing my head with great joy unto that mass of energy and effulgence, I said these words unto the great Bhagavan Shiva, with a heart filled with gladness,--
1) Firmness in virtue, (dharme dṛḍhatvaṃ)
2) The slaughter of foes in battle,(yudhi śatrughātaṃ)
3) The highest fame,(yaśas tathāgryaṃ )
4) The greatest might, (paramaṃ balaṃ)
5) Devotion to Yoga, (yogapriyatvaṃ)
6) Thy adjacence, (ava saṃnikarṣaṃ)and
7) Hundreds upon hundreds of children, (vṛṇe sutānāṃ ca śataṃ śatāni)
these are the boons I solicit of thee,--So be it,--said Sankara
tato māṃ jagato mātā dharaṇī sarvapāvanī
uvācomā praṇihitā śarvāṇī tapasāṃ nidhiḥ
After this, the mother of the universe, the upholders of all things, who cleanses, all things, viz., the spouse of Sarva, that vast receptacle of penances said with a restrained soul these words unto me,--
datod bhagavatā putraḥ sāmbo nāma tavānagha
matto 'py aṣṭau varān iṣṭān gṛhāṇa tvaṃ dadāmi te
praṇamya śirasā sā ca mayoktā pāṇḍunandana
'The puissant Mahadeva has granted thee, O sinless one, a son who shall be named Samba. Do thou take from me also eight boons which thou choosest. I shall certainly grant them to thee.-- Bowing unto her with a bend of my head, I said unto her, O son of Pandu,--
dvijeṣv akopaṃ pitṛtaḥ prasādaṃ; śataṃ sutānām upabhogaṃ paraṃ ca
kule prītiṃ mātṛtaś ca prasādaṃ; śama prāptiṃ pravṛṇe cāpi dākṣyam
I solicit from thee non-anger against the Brahmanas, grace of my father, a hundred sons, the highest enjoyments, love for my family, the grace of my mother, the attainment of tranquillity and peace, and cleverness in every act!'
evaṃ bhaviṣyaty amaraprabhāva; nāhaṃ mṛṣā jātu vade kadā cit
bhāryā sahasrāṇi ca ṣoḍaśaiva; tāsu priyatvaṃ ca tathākṣayatvam
"Uma said, 'It shall be even so, O thou that art possessed of prowess and puissance equal to that of a celestial. I never say what is untrue. Thou shalt have sixteen thousand wives. Thy love for them and theirs also for thee shall be unlimited.
prītiṃ cāgryāṃ bāndhavānāṃ sakāśād; dadāmi te vapuṣaḥ kāmyatāṃ ca
bhokṣyante vai saptatir vai śatāni; gṛhe tubhyam atithīnāṃ ca nityam
From all thy kinsmen also, thou shalt receive the highest affection. Thy body too shall be most beautiful. Seven thousand guests will daily feed at thy palace.'
evaṃ dattvā varān devo mama devī ca bhārata
antarhitaḥ kṣaṇe tasmin sagaṇo bhīma pūrvaja
etad atyadbhutaṃ sarvaṃ brāhmaṇāyātitejase
upamanyave mayā kṛtsnam ākhyātaṃ kauravottama
Vasudeva continued, 'Having thus granted me boons both the god and the goddess, O Bharata, disappeared there and then with their Ganas, O elder brother of Bhima. All those wonderful facts I related fully, O best of kuru kings, to that Brahmana of great energy, viz., Upamanyu (from whom I had obtained the Diksha before adoring Mahadeva).
namaskṛtvā tu sa prāha devadevāya suvrata
nāsti śarva samo dāne nāsti śarva samo raṇe
Bowing down unto the great God, Upamanyu said these words to me.':
"Upamanyu said, 'There is no deity like Sarva. There is no end or refuge like Sarva. There is none that can give so many or such high boons. There is none that equal him in battle.'"