I have heard Ashwatthama who is an important character in the story in the Mahabharata epic and one of the chiranjeevas (immortals) to be the twelfth Rudra. However, he is seen as a cruel person for having the killing all the sons of the Pandavas and siding with the evil Kauravas. My question is how can such an evil person be the partial incarnation of the great Lord Shiva?
First of all Ashwatthama is not purely Shiva Avatar, he is mixed incarnation as told in this chapter of Mahabharata:
And, O king, his son the heroic Aswatthaman, of eyes like the lotus-petals, gifted with surpassing energy, and the terror of all foes, the great oppressor of all enemies, was born on earth, of the united portions of Mahadeva, Yama, Kama, and Krodha.
Secondly, it is Lord Mahadeva himself who possessed Ashwatthama and created all destructions which happened in that night of Mahabharata. It is seen from this chapter of Saupitaka Parva:
सत्यशौचार्जव त्यागैस् तपसा नियमेन च ।
क्षान्त्या भक्त्या च धृत्या च बुद्ध्या च वचसा तथा ।।
यथावद् अहम् आराद्धः कृष्णेनाक्लिष्टकर्मणा।
तस्माद् इष्टतमः कृष्णाद् अन्यो मम न विद्यते।।
कुर्वता तस्य संमानं त्वां च जिज्ञासता मया।
पाञ्चालाः सहसा गुप्ता मायाश् च बहुशः कृताः।।
कृतस् तस्यैष संमानः पाञ्चालान् रक्षता मया।
अभिभूतास् तु कालेन नैषाम् अद्यास्ति जीवितम् ।।
“एवम् उक्त्वा महेष्वासं भगवान् आत्मनस् तनुम्।
आविवेश ददौ चास्मै विमलं खड्गम् उत्तमम् ।।
अथाविष्टो भगवता भूयो जज्वाल तेजसा।
वर्ष्मवांश् चाभवद् युद्धे देव सृष्टेन तेजसा ।।
"With truth, purity, sincerity, resignation, ascetic austerities, vows, forgiveness, devotion, patience, thought, and word, I have been duly adored by Krishna of pure deeds. For this there is none dearer to me than Krishna. For honouring him and at his word I have protected the Pancalas and displayed diverse kinds of illusion. By protecting the Pancalas I have honoured him. They have, however, been afflicted by time. The period of their lives hath run out." Having said these words unto the high-souled Ashvatthama, the divine Mahadeva entered Ashvatthama's body after giving him an excellent and polished sword. Filled by that divine being, Drona's son blazed up with energy. In consequence of that energy derived from godhead, he became all-powerful in battle. Many invisible beings and rakshasas proceeded along his right and his left as he set out, like the lord Mahadeva himself, for entering the camp of his foes.
So, it is Lord Mahadeva himself becoming Mahakala who destroyed that night. They were destroyed because their time for destruction had come. Same thing is told by Lord Krishna to Yudhisthira in this chapter of Saupitaka Parva:
Yudhishthira in great grief said these words unto him of Dasharha's race: ‘How, O Krishna, could my sons, all of whom were mighty car-warriors, be slaughtered by the sinful and wretched Ashvatthama of no great skill in battle? How also could Drona's son slay the children of Drupada, all of whom were accomplished in weapons, possessed of great prowess, and capable of battling with hundreds of thousands of foes? How could he slay that foremost of car-warriors, Dhrishtadyumna, before whom the great bowman Drona himself could not appear? What act was done by the preceptor's son, O bull among men, in consequence of which he succeeded in slaying, single-handed, all our men in battle?'
Vasudeva said, "Verily, Drona's son had sought the aid of that highest of all the gods, the eternal Mahadeva. It was for this that he succeeded in slaying, single-handed, so large a number of warriors. If Mahadeva be gratified, he can bestow even immortality. Girisha can give such valour as will succeed in checking Indra himself. I know Mahadeva truly, O bull of Bharata's race! I know also his various acts of old. He, O Bharata, is the beginning, the middle, and the end of all creatures.
So, Ashwatthama is just a medium for the destruction. It was Lord Mahadeva who did that task. One may hold Ashwatthama also somehow responsible for invoking destruction. But the thing is as Mahadeva says 'They are afflicted by time.' So, they must die.
The reason why Lord Shiva goes to Ashwatthama is because he fully surrendered upon Lord Rudra:
Ashvatthama said, "Sprung from Angirasa's line, I am about to pour my soul, O god, as a libation on this fire! Accept, O lord, this victim! In this hour of distress, O Soul of the universe, I offer up my own self as the sacrificial victim, from devotion to thee and with heart concentrated in meditation! All creatures are in thee and thou art in all creatures! Assemblage of all high attributes occur in thee! O lord, O thou art the refuge of all creatures. I wait as a libation for thee, since I am unable to vanquish my foes. Accept me, O god." Having said these words, Drona's son, ascending that sacrificial altar on which a fire blazed brightly, offered himself up as the victim and entered that blazing fire.
One may feel that Lord Rudra is doing injustice there by creating such havoc in the camp of Pandava. But Lord Rudra was in his Mahakala aspect at that time and he treated equally. Mahabharata also in this chapter of Shanti Parva states that Lord Rudra comes and annhilates totally there when sins increase in some place:
In consequence of the sins perpetrated by sinful men, the god Rudra appears in the kingdom. Indeed, the sinful by their sins bring upon them that god of vengeance. He then destroys all, the honest and the wicked alike (without making any distinction). '
"Aila said, 'Whence does Rudra spring? What also is his form? Creatures are seen to be destroyed by creatures. Tell me all this, O Kasyapa! Whence does the god Rudra spring?'
"Kasyapa said, 'Rudra exists in the hearts of men. He destroys the bodies themselves in which he dwells as also the bodies of others. Rudra has been said to be like atmospheric visitations and his form is like that of the wind-gods.'
So, it is a common thing that when sins accumulate in a place Lord Rudra comes in his Mahakãla aspect and annhilates all. Actually it is Lord Shiva himself in his Mahakãla aspect who destroyed warriors of Kurukshetra too as described in this chapter of Drona Parva.
The SataRudriya Hymn of Yajurveda too states that it is Lord Rudra himself who torments (as per Karma of beings):
नम आक्खिदते च प्रक्खिदते च । (9th Anuvaka)
Salutations to him who troubles little and to him who troubles most.
So, the Gracer is also Lord Shiva and troubler is also Lord Shiva. His portion of Ashwatthama served like troubler during the time of Mahabharata.