I don't know how any pirates did it, but I can tell you how the British navy did it during the age of sail.
The powder magazine was the lowest and most interior part of the ship. Being below the waterline and that far in made it extremely safe from enemy fire. Still a ship could blow up by being hit in the magazine. The most famous of these would be the French flagship, Orient, which exploded at the battle of the Nile. It was most likely a magazine explosion. If it was from enemy fire or accident, we will probably never know.
During battle, the Gunner, a Warrant Officer, would don special footwear called list slippers, so as not to strike a spark on the deck. Then he would go into the magazine. The only light he would have to work with came from a lantern hung outside the magazine. Between the magazine and the rest of the ship hung curtains which would be kept wet and through which the gunner would pass the measured powder charges to the powder monkeys.
Sources: David Howarth: Trafalgar, The Nelson Touch and all the Aubrey-Maturin novels by Pat O'Brian.