In the chapter 36, Section 3 of Satarudra-Samhita of Shiva Purana, the story of incarnation of Shiva as Ashvattaman is mentioned.
A part of the celestial sage of great intellect Bṛhaspati, the self-possessed and prudent Droṇa was born of Bharadvaja but not of any womb. He was the foremost among the archers, heroes, a brahminical sage, well-versed in all lore, of great fame and brilliance and the most excellent of all the wielders of weapons and missiles. Learned men knew Droṇa as the most excellent, skilful in the lore of archery as well as the Vedas, of variegated activities, and as one who made his race flourish. He was the instructor of the Kauravas. He was the most well known among the six Maharathas among the Kauravas.
The most excellent BrahminDroṇa performed an elaborate penance with Shiva in view for obtaining a son in order to help the Kauravas. The delighted lord Shiva appeared in front of Droṇa. On seeing him, the Brahmin Drona whose heart was highly delighted bowed to him with palms joined in reverence and eulogised him. Satisfied and pleased with his eulogy and penance, lord Shiva, favourably disposed towards his devotees told Droṇa—“Mention the boon you wish to choose”.
On hearing the words of Shiva, Droṇa bowed low and said—“Please give me a son born of your part who will be invincible to every one and who will be very powerful. On hearing the words of Droṇa, Shiva said, “So be it.”
Droṇa returned to his abode highly delighted and free from illusion. He joyously narrated all the details to his wife. Taking the due opportunity, lord Shiva, the annihilator of all and very powerful was born as Droṇa’s son through a part of himself.
He became well known as Aśvatthaman on the earth. He was heroic, had lotus-like eyes and he could reduce the strength of his enemy. Securing the permission and behest of his father that famous hero helped the Kauravas in the Mahabharata war. By resorting to that powerful hero, Bhisma and other Kauravas became invincible even to the gods. Fearing him, the Paṇḍavas, though they were great heroes and very skilful, became incapable of conquering the Kauravas.
At the instance of Krishna, Arjuna, performed the severe penance of Shiva and obtained a divne missile duo to Shiva’s boon. He then conquered the Kauravas, though the great hero Ashvatthaman was born of a part of lord Shiva.
Due to his devotion to the Kauravas, he exhibited his full vigour. He destroyed the sons of Paṇḍavas though they were trained in warfare by Krishna and other great heroes. He had the strength that could not be withstood by the enemy. On seeing Arjuna dejected by the death of his son but rushing on him along with Krishna he fled away from the battle-field. He discharged the divine missile Brahmastiras on Arjuna. Then a fierce splendour enveloping all the quarters manifested itself. Sensing danger to his own life Arjuna was much distressed. Losing all his brilliance and highly frightened he spoke to Krishna becoming utterly exhausted. Arjuna said:--
“O Krishna, what is this? Whence it has arisen I do not know. This unbearable fiery splendour comes from all sides. On hearing the words of Arjuna, Krishna the most excellent among the devotees of Shiva meditated on Shiva and Parvati and then spoke to Arjuna.”
“Know this to be the most powerful Brahma missile of Ashvatthaman. There is no other missile to counteract this. Immediately think of Shiva, your lord, the protector of the devotees who had given you a weapon capable of every action. Counteract the fiery splendour of the Brahma missile with your Saiva missile.”
After saying this Krishna himself meditated on Shiva for that purpose. On hearing the words of Krishna, Arjuna remembered Shiva, touched water, bowed to Shiva and discharged the Saiva missile. Although the Brahmastiras missile never goes futile and cannot be counteracted, it subsided before the splendour of the Saiva missile. Knowing that this happened by the influence of Shiva, Ashvatthaman, born of Shiva’s part and delighted in heart at Shiva’s wish, was not at all sorry.
Then the son of Droṇa wishing the world to be rid of Pandava lineage desired to kill the child in the womb of Uttara. The powerful Ashvatthaman hurled the excessively lustrous Brahma-missile that could not be countermanded by any other missile, aiming it at the womb of Uttara. Then Uttara the daughter-in-law of Arjuna, burning within due to that missile and agitated in the mind eulogised Krishna. Then meditating on Shiva mentally, bowing to and eulogising him, Krishna understood that to be the missile of the son of Droṇa. By means of his missile—the brilliant Sudarshana, bestowed on him by Shiva for his protection, he saved her at the behest of Shiva.
Ashvatthaman then knew that Krishna had assumed his form at the behest of Shiva and did it as he was the foremost among the devotees of Shiva, slowly refrained from the thought of destroying Uttara. Then the leading devotee of Shiva, the delighted Krishna, made all the Paṇḍavas fall at the feet of Ashvatthaman for pleasing him. Then Ashvatthaman, son of Drona, highly delighted in the mind, blessed the Pandavas and Krishna with various boons.
Incarnating thus on the earth in the form of Droṇa’s son, lord Shiva performed various sports. The incarnation of Shiva Ashvatthaman of great strength and exploit is even now alive on the banks of the river Ganga bestowing happiness on the three worlds.