The ending of this play is comedic because Olivia marries Sebastian, Viola’s brother, thinking that she is marrying Cesario, who is really Viola pretending to be a man. However, Olivia has really been mourning for her brother this entire time, which is why it was sad.
Love really is a two sided coin of comedy and tragedy. You cannot have one without the other. The ending of this play, which I suppose you could call a romantic comedy, is funny and a little poignant. Olivia finds out that the “man” she thought she was in love with the entire time was really a woman, who was trying to get her to fall in love with the Duke Orsino. It didn't work. She mistook Cesario’s sensitive nature for an interest in her. When her brother turned up, she married him, thinking she was marrying her, or him. You get the idea.
Also, the duke finally comes to his senses and realizes that he is actually in love with Cesario when he realizes that "he" is a "she" and not just a sensitive youth who is really good at reading women because he is one! It is a quick courtship. He decides to leave Olivia alone, and marry Viola instead. Happy endings for all!
While the marriage mix-up is laugh out loud funny it is also kind of tragic because poor Viola thought this entire time that her brother Sebastian was dead, in the shipwreck that landed her in Illyria in the first place. Viola agreed to help the Duke make Olivia fall in love with him (even though Olivia did not want to marry anyone since she was sad about her brother dying) because she was a good-hearted girl. However, Viola did this disguised as a boy.
It does work out for Viola though. She gets to marry a duke!
Be not amazed; right noble is his blood.
If this be so, as yet the glass seems true,
I shall have share in this most happy wrack.—
Boy, thou hast said to me a thousand times
Thou never shouldst love woman like to me.(Act 5, Scene 1)
Of course, this is tongue in cheek when he calls her boy now, because he knows she is not. All in fun! He can't wait to see her in "woman’s weeds" and marry her.
In the end, everyone ends up with the right person (and the right gender), and the duel is not deadly. Shakespeare's message seems to be that while love might be a little treacherous, it is worth it in the end. Also, you might think you love one person, when the person you should really love is right in front of you the entire time!