The subplot had several purposes; it is a contextual and dramatic device, adds comedy, is a metaphorical parallel of the main plot and is a satire on the Puritanical hold on contemporary society.
To address one aspect of why it was included in the play; the comic subplot is a contextual device and was a common occurrence in Renaissance plays (Renaissance Culture in Context: Theory and Practice, by Jean R. Brink, William F. Gentrup, p.54); it can be seen in some of Shakespeare's other plays, The Tempest, with Caliban, Trinculo and Stephano, Measure for Measure, Falstaff in Henry IV Part 1, two subplots (Oberon and Titania's quarrel and Bottom turned into a donkey) in A Midsummer Night's Dream as well as Wagner and the Three Scholars in Marlowe's Doctor Faustus. Also contextually, Shakespeare's theatres were often rowdy and attracted all social classes, and some only attended the theatre for straightforward entertainment (as oppose to the serious, thought-provoking love stories between the other characters); hence the need for the drama (the main plot is somewhat slow-burning and so the subplot maintains action) and comic, slapstick buffoonery of Sir Toby, Sir Andrew and Maria.
As you say it is mainly to add comedy to the comedy that is Twelfth Night. This is because the main plot isn't exactly a light or funny one: Viola believes her brother was killed in the shipwreck at the beginning and she is left vulnerable and alone to survive in a strange country. She falls in love with her employer, Orsino, and is unrequited for a large part of the play due to her disguised identity. She describes her emotional suffering from both the supposed death of her brother and her love for Orsino to the oblivious Orsino:
She sat like patience on a monument,
Smiling at grief
Really, this main storyline is rather quite dark, as you say. So the subplot acts as a release and also contrast to this when Malvolio is deceived by a forged love note which leads to an awkward but amusing confusion when he believes Olivia loves him and then acts accordingly. (Reference)
In addition, the subplot acts as a parallel to the main plot. Self-love and misguided infatuation are featured in both: Orsino really is more in one with the idea of himself being in love, evident in the opening lines:
If music be the food of love, play on;
Give me excess of it
and Malvolio is excessively proud and pretentious when he believes Olivia loves him, with her even commenting directly:
You are sick of self love, Malvolio
The two plots also share strong, independent female characters, Viola in the main plot and Maria in the subplot. Viola has managed to survive the shipwreck and secure herself in Illyria and Maria was the brain behind the scheme to trick Malvolio. Both women find love in the end also; Maria and Sir Toby and Viola and Orsino.
Therefore, the subplot supports the main plot by unifying itself through shared, central subjects as well as adding complexity to an otherwise somewhat simple story arc of love and losing and finding a brother.
Lastly, the trickery and imprisonment that Maria, Toby and Andrew inflict on Malvolio is a reaction to his Puritanical behaviour:
Dost thou think, because thou art virtuous, there shall be no more cakes and ale?
This darker part of the subplot is an anti-Puritan satire by Shakespeare, as 19th century critic, Joseph Hunter commented:
in Malvolio he [Shakespeare] has made the Puritan at once detestable and ridiculous
Malvolio's constant reprimands to the other characters' drinking and feasting symbolises the Puritans' disapproving view on the twelfth night of Christmas' festivities and the other characters' treatment of him mirrors the Elizabethans' view of Puritanism; Yes, it could be seen as cruelty and the audience may sympathise with Malvolio, but this is mainly an individual decision.