Man, Twain loved the Bard. There the duke and king rehearsing on the boat with an odd amalgamation (now there's a $5 word that just means "mix") of the greatest soliloquies of all time in one totally messed-up speech, the unsuccessful performance in front of the "uncivilized" folk of Arkansas, and the subsequent naked prancing about. (Not to mention the Romeo and Juliet-esque debacle with the Shepherdsons and Grangerfords.)
So what's up with that?
Well, the duke and king are all wrapped up in their own sophistication. They think they're just so worldly and clever. Of course, the joke is on them for being ignoramuses who use words like "orgies" to talk about funeral rites (25). It's the same thing with the Shakespeare; the duke thinks he knows what he's talking about, but he's really going around saying: "To be or not to be; that is the bare bodkin" (21).
All the references to Shakespeare might just be one way for Twain to make fun of the duke and king's pretensions. But it's also a way for Twain to contrast the smart but uneducated Huck with the duke and king—who think they're educated but actually turn out to be full of hot air… and badly quoted Shakespeare.