The confusing scene between Malvolio and Viola happens after "Cesario" has just met with Olivia (II.ii). Olivia uses Malvolio as a pawn in a trick to get Cesario to come back and talk with her again another day. Viola does tell the truth that she did not leave a ring with Olivia, but Malvolio won't believe it because he assumes that Olivia would never lie. Also, as a servant, he is supposed to carry out his orders, so he leaves the ring on the ground for Cesario/Viola to do with as she sees fit under the circumstances. Viola realizes Olivia's game after Malvolio leaves:
"She loves me, sure; the cunning of her passion
Invites me in this churlish messenger"(II.ii21-22).
Olivia explains her purposes behind sending Malvolio with the ring to Cesario in the following passage:
"Give me leave, beseech you. I did send,
After the last enchantment you did here,
A ring in chase of you: so did I abuse
Myself, my servant and, I fear me, you:
Under your hard construction must I sit,
To force that on you, in a shameful cunning,
Which you knew none of yours" (III.ii.109-115).
Olivia proves with this measure of cunning that she really does not want the Duke for a husband and that she has fallen for Cesario! Now, since Cesario has the ring that belongs to Olivia, he must return it to her--and that's exactly what she wants.