The story of Lord Shiva and Tilottama is told by Lord Shiva himself in this chapter of Anushashana Parva. Lord Shiva himself tells that it is just to show the power of Yoga:
तिलोत्तमा नाम पुरा ब्रह्मणा योषिदुत्तमा ।
तिलं तिलं समुद्धृत्य रत्नानां निर्मिता शुभा ।।
साभ्यगच्छत मां देवि रूपेणाप्रतिमा भुवि ।
प्रदक्षिणं लोभयन्ती मां शुभे रुचिरानना ।।
यतो यतः सा सुदती मामुपाधावदन्तिके ।
ततस्ततो मुखं चारु मम देवि विनिर्गतम्।।
तां देदृक्षुरहं योगाच्चतुर्मूर्तित्वमागतः ।
चतुर्मुखश्च संवृत्तॊ दर्शयन योगमात्मनः।।
'In days of yore, a blessed woman was created by Brahman, called Tilottama, by culling grains of beauty from every beautiful object in the universe. One day, that lady of beautiful face, unrivalled in the universe for beauty of form, came to me, O goddess, for circumambulating me but really compelled by the desire of tempting me. In whatever direction that lady of beautiful teeth turned, a new face of mine instantly appeared. All those faces of mine became agreeable to look at. Thus, in consequence of the desire of beholding her, I became four-faced, through Yoga-puissance, Thus, I showed my high Yoga-power in becoming four- faced.
Also these four faces shows the attributes of Lord which are also described in SataRudriya section of Yajurveda. Like "Bhavãya cha / who is the Origin", "Somãya cha/who is with Uma", "Rudrãya cha/ who ends the Cries", "Sarvãya cha / who destroys everything", etc.. Lord Shiva himself explains the functions of his four faces there as:
पूर्वेण वदनेनाहम इन्द्रत्वमनुशास्मि ह ।
With that face of mine which is turned towards the east, I exercise the sovereignty of the universe.
उत्तरेण त्वया सार्धं रमाम्यहमनिन्दिते ।।
With that face of mine which is turned towards the north, I sport with thee, O thou of faultless features!
पश्चिमं मे मुखं सौम्यं सर्वप्राणि सुखावहम् ।
That face of mine which is turned towards the west is agreeable and auspicious. With it I ordain the happiness of all creatures.
दक्षिणं भीमसंकाशं रौद्रं संहरति प्रजाः ।।
That face of mine which is turned towards the south is terrible. With it I destroy all creatures.
Thus these are the functions of four faces. These functions are always carried out by Lord Shiva but during Tilottama event he revealed upto 4 faces only and on being asked by Uma he replied the secret of four faces. Thus, Lord Shiva through his simple Leela displayed powers of Yoga and attributes through which he rules (East face), sports (North face), protects (West face) and destroys (South face) the whole world.
The story of Lord Shiva and Madhura is just a folklore and it is highly unlikely that it is mentioned in some scriptures. But still Folklores have their own justifications.
Upotghatapaada Brahmanda Purana Chapter 7 states:
इत्यते बहुसाहस्रा भास्वरा अप्सरोगणाः ।
देवतामामृषीणां च पत्न्यश्च मातरश्च ह ।।
सुगंधाश्चाथ निष्पंदा सर्वाश्चाप्सरसः समाः ।
संप्रयोगस्तु कामेन माद्यं दिवि हरं विना ।।
तासां देवर्षी संस्पर्शा जाताः साधारणा यतः ।
These brillant groups of Apsaras numbering many thousands are the wives and mothers of Gods and Sages. All these Apasars are equally fragrant and free from excitement. Except Hara, everyone from among the Devas and Sages had contact with them on account of Kãma since they were common to all.