Shakespeare's Twelfth Night ends neatly because most of the confusion is resolved and the union of the main characters is fairly well established.
The plot and conclusion of Twelfth Night satisfy the clarification of comedy made by Northrop Frye, who defines comedy as being
community-oriented, [and] its vision has social significance. This vision calls for the establishing of society as we would like it. (Frye, 286)
To begin with in Shakespeare's comedy of character, there is activity that is confused or thwarted:
- Viola, who is shipwrecked, finds herself on the shore of Illyria. She believes that her beloved twin brother Sebastian has died at sea; however, the sea-captain Antonio tells her that he saw her brother had strapped himself to a mast, so there is a chance that he is yet alive.
Viola decides to disguise herself as a man named Cesario and finds employment with Duke Orsino. When she carries a message of love from the Duke to Lady Olivia, Olivia becomes infatuated with Cesario. Viola's disguise has thus entrapped her. She cannot tell Olivia that she is not a man. Later, when she falls in love with the Duke, Viola finds herself in a quandary because she is the messenger of love for the Duke and he believes she is a male.
- Duke Orsino is in love with Olivia, but she is not interested in any romantic relationship because her brother has died and she has decided to mourn for her beloved sibling for seven years. However, when Cesario brings a message of love from the Duke, Olivia finds herself falling in love with the messenger. Viola/Cesario is unnerved that Olivia has become infatuated with her false personality.
- Olivia's uncle, Sir Toby Belch, invites his friend Sir Andrew Aguecheek to join him at Olivia's house. Sir Andrew tries to court Olivia, who is clearly not interested in him because he is rather dim-witted. Sir Toby exploits him for his money.
- Olivia's servant Malvolio, who is rather prudish, tries to prevent Sir Andrew and Sir Toby Belch from engaging in raucous activity. In retaliation, Maria, Olivia's waiting-gentlewoman, and Sir Toby lock Malvolio in a dark room, where Malvolio suffers, but he refuses to believe that he is not sane as his captors would have him believe.
- Sebastian, Viola's twin brother, is rescued by the sea-captain Antonio, who gives Sebastian money and takes him to Illyria.
At the play's end, most of the confusion is ended and conflicts are resolved.
- Sebastian's appearance at the end allows Viola to solve her dilemmas:
--Olivia, who is in love with Viola's masculine persona, thinks she marries Cesario, but it is Viola's twin Sebastian who becomes her husband. Nevertheless, Olivia is satisfied.
--Thus, Viola is free to show Orsino that she is, in fact, a woman. Remembering how many times Cesario declared "his" love for him, Duke Orsino comprehends the true meaning of these words and accepts Viola.
- Sir Toby is so impressed with Maria's cleverness that he asks her to marry him, and she agrees.
- Sir Andrew and Malvolio seem to be the only characters whose situation at the end of the play is not as positive as in the beginning because both men have been exploited. Malvolio has been left locked up for some time. When he is finally released, he declares, "I'll be revenged on the whole pack of you" (5.1.168). But at least he has been freed. Sir Andrew, also, has been used by Sir Toby for his money. He is also harmed as he is forced into a duel and is injured. But his trials are over and he intends to depart.
Note: Relevant to the question, in an essay entitled "Chaos and Order," it is written that Viola is
...a heroine who brings with her the power for a restorative balance to the wild swings of mood, temperament, and behaviour inherent in Illyrian society. (eNotes Critical Essays)