The device of mistaken identity is one Shakespeare probably derived from Roman comedy, where it is part of stock humorous plots. If one reflects on the device, though, it is essential to the nature of theatrical experience. To watch a play, we must "mistake" the actors for the characters they are playing. Thus all mistaken identity in dramatic plots takes advantage of two levels of mistake, of actor A playing part B in which character B plays character C. Often the jokes about mistaken identity thus have a dual reference to both the character's and actor's situation.
In both classical and Shakespearean drama, female characters were played by men. A favourite form of dual identity becomes the male actor playing a woman playing a man, a part that requires some acting skill as the male actor can't simply act normally as a man, but must exaggerate the feminine while in male disguise.