Yes, the bit in Master and Commander was based upon the real life action between the 14-gun H.M.B Speedy and a Spanish 32-gun Xebec-frigate named El Gamo in 1801.
The British commander, Lord Thomas Cochrane, pulled off a series of bluffs to allow his ship to get along side. The Spanish captain was supposedly killed by the first broadside fired by the Speedy and so his ship was in disarray when the British came alongside. Cochrane's entire crew then boarded (with the exception of the ship's surgeon). The bulk of the Spanish ship's crew were on the lower gun deck, so the British attackers were fighting only a similar number of men on the upper deck.
Cochrane then bluffed again, calling for more men to be sent up from the Speedy (there weren't any more to be sent) and also had one of his own men lower the Spanish flag, which was the usual symbol of a ship's surrender. The bluff(s) worked and the Spanish vessel was taken.
You might also want to take a look at some of the "cutting out" operations performed during that period. These were operations where a ship's boats (carrying marines and a proportion of the crew) were used to capture larger vessels in a harbour or anchorage.
A fine example of "cutting out" was the re-capture of HMS Hermione from the Spanish in Puerto Cabello. Again you had a big difference in manpower on each side (100 British against 400 Spanish) and again the loss of the Spanish captain proved a decisive point.
In respect of why it's possible to capture a warship with a small boarding party, it's essentially down to the design of ships of the period. The top deck (or weatherdeck) of a square-rigged sailing ship was the bridge and engine room combined.
By taking control of the top deck, you controlled the sails and, therefore, controlled the movement of the vessel. The main armament of a sailing warship of the time would be along the sides, with very little ability to fire forward or aft. So if you could immobilize a vessel it would be a sitting duck for any other ship, even a much smaller one, who could maneuver into a position across the bow or stern and attack with little risk to themselves.
Also the limited points of access from the lower decks could cause bottlenecks that would prevent large numbers of defenders coming from below at the same time. So, once the top deck had been taken by a boarding party, any defender counter-attacking from below could find himself fighting multiple opponents (who had more room) and, at the same time, blocking his own crew mates from coming to his aid.
As a consequence of these factors, it was usual for a ship's crew that had lost control of the top deck to surrender the ship, even if they were still numerically superior to the boarding party.