It's based on The Prince and the Pauper, published in 1881 and written by Mark Twain. The theme has been used quite a few times since then in various forms, including, I think, The Parent Trap.