The British warship H.M.S. Pinafore is at anchor off Portsmouth. The sailors are on the quarterdeck, proudly "cleaning brasswork, splicing rope, etc."
Little Buttercup, a Portsmouth "bumboat woman" (dockside vendor) - who is the "rosiest, roundest, and reddest beauty in all Spithead" - comes on board to sell her wares to the crew. She hints that she may be hiding a dark secret under her "gay and frivolous exterior". Ralph Rackstraw, "the smartest lad in all the fleet", enters, declaring his love for the Captain's daughter, Josephine. His fellow sailors (excepting Dick Deadeye, the grim and ugly realist of the crew) offer their sympathies, but they can give Ralph little hope that his love will ever be returned.
Sir Joseph enters and complains that Josephine has not yet agreed to marry him. The Captain speculates that she is probably dazzled by his "exalted rank" and that if Sir Joseph can persuade her that "love levels all ranks", she will accept his proposal. They withdraw, and Josephine enters, still feeling guilty about her planned elopement with Ralph and fearful of giving up a life of luxury. When Sir Joseph makes the argument that "love levels all ranks", a delighted Josephine says that she "will hesitate no longer". The Captain and Sir Joseph rejoice, but Josephine is now more determined than ever to marry Ralph.
Dick Deadeye intercepts the Captain and tells him of the lovers' plans to elope. The Captain confronts Ralph and Josephine as they try to leave the ship. The pair declare their love, justifying their actions because "He is an Englishman!" The furious Captain is unmoved and blurts out, "Why, damme, it's too bad!" Sir Joseph and his relatives, who have overheard this oath, are shocked to hear swearing on board a ship, and Sir Joseph orders the Captain confined to his cabin.
When Sir Joseph asks what had provoked the usually polite officer's outburst, Ralph replies that it was his declaration of love for Josephine. Furious in his turn at this revelation, and ignoring Josephine's plea to spare Ralph, Sir Joseph has the sailor "loaded with chains" and taken to the ship's dungeon. Little Buttercup now comes forward to reveal her long-held secret. Many years ago, when she "practised baby-farming", she had cared for two babies, one "of low condition", the other "a regular patrician". She confesses that she "mixed those children up .... The wellborn babe was Ralph; your Captain was the other."
Sir Joseph now realises that Ralph should have been the Captain, and the Captain should have been Ralph. He summons both, and they emerge wearing one another's uniforms: Ralph as Captain, in command of the Pinafore, and Corcoran as a common sailor. Sir Joseph's marriage with Josephine is now "out of the question" in his eyes: "love levels all ranks ... to a considerable extent, but it does not level them as much as that." He hands her to Captain Rackstraw. The former Captain's now-humble social rank leaves him free to marry Buttercup. Sir Joseph settles for his cousin Hebe, and all ends in general rejoicing.