The quote is a parody of the folklore motif known as the king in disguise. In Norse mythology Odin was said to wander in disguise among humans. Shakespeare used the king-in-disguise motif in Act 4, scene 1 of Henry V. Outside of fiction, a number of real kings and queens have been said to disguise themselves, e.g. King Charles XI of Sweden (1655 – 1697), who supposedly wanted to check for himself whether local officials were corrupt and suppressed the people. (A blog on Weebly.com is not the most reliable source, but stories that aren't based on true facts are still valid illustrations of a specific motif.)
The statement that it "always turns out to be the King of Sweden" might be an allusion to King Charles XI of Sweden, but the key part is the adverb "always", which is a comic exaggeration, since Charles XI is not the only royal in history who disguised himself.
The quote may also parody a motif found in legends and fairy tales: a character meets an unassuming or even unattractive person and their behaviour towards that person turns out to have an unexpected effect.
An example from Arthurian legends is the story of Gawain and the loathly lady, for example in The Marriage of Sir Gawain or The Wedding of Sir Gawain and Dame Ragnelle. In the latter story, Gawain must consent to marry an "old hag" in order to solve a serious problem that King Arthur is confronted with. Quoting from Wikipedia:
Later, the newlyweds [Gawain and the loathly lady] retire to their bedroom. After brief hesitation, Gawain assents to treat his new bride as he would if she were desirable, and go to bed with her as a dutiful husband is expected to do. However, when he looks up, he is astonished to see not an ugly hag, but the most beautiful woman he has ever seen standing before him.
Another variation on the same theme is The Frog Prince, which is best known in the version preserved by the brothers Grimm (emphasis added):
In the tale, a spoiled princess reluctantly befriends the Frog Prince, whom she met after dropping a gold ball into a pond, and he retrieves it for her in exchange for her friendship. The Frog Prince magically transforms into a handsome prince. In the original Grimm version of the story, the frog's spell was broken when the princess threw it against the wall, while in modern versions the transformation is triggered by the princess kissing the frog.