If you dig deep enough into the possible source materials that Shakespeare used for Twelfth Night, you can find a whole slew of suspects.
You want to talk twins and the confusion that can arise from mistaken identity (like The Comedy of Errors)? Check out the old Greek play, Plautus’ Menaechmi, which actually was a source of that earlier play).
If, however, you want to talk about the concept of the female disguising herself as a young male, to work for a(nother) male, who tasks her/him to plead his suit to a(nother) female, who in turn falls in love with the first female, but as the young male, who–of course–has fallen for the male who has hired her (as a him)… well, then you’ve got quite the tangle coming up.
Many believe that Shakespeare took as one of his sources “Apolonius and Silla,” a story in a collection of stories called Riche his Farewell to Militarie Profession conteining verie pleasaunt discourses fit for a peaceable tyme, by Barnabe Rich. Rich was about one generation older than Shakespeare and the collection was published around 1581, so Shakespeare would have been aware of it. In “Apolonius and Silla,” you get the shipwreck, the loss of a brother, the assumption of the male disguise.
But just as Shakespeare was a master appropriator, so was his fellow Englishman Rich, as “Apolonius and Silla” was based, some say, on Matteo Bandello’s story of Nicuola and Lattantio in his work Nouvelle, though probably not directly through Bandello’s Italian text from the 1550’s, but rather through the French translation by François de Belleforest’s in the early 1570’s.
Of course, even Bandello’s story isn’t wholly original…
In the late 1520’s, the Accademia degli Intronati was founded near Sienna, Italy. Originally just a place for the aristocracy to meet, it would later become a hub for intellectuals. In the early 1530’s, the Intronatis, as the members of the group were known, collectively wrote a comedic play called Gl’ingannati (The Deceived Ones). It is this play that served as Bandello’s source.
Only in this original, not only do you get the heroine’s disguise as a male, AND the falling of love of the heroine with her new master, but you also get the confusion then resolution of the conflict by the appearance of the heroine’s twin brother. Since there are some aspects of Gl’ingannati, that hew more closely to Twelfth Night than do Nouvelle or “Apolonius and Silla,” some believe that Shakespeare may have been influenced directly by Gl’ingannati.
We’re probably never going to know for certain.