Soft-gamma-ray repeaters (SGRs) are galactic X-ray stars that emit numerous short-duration (about 0.1 s) bursts of hard X-rays during sporadic active periods. They are thought to be magnetars: strongly magnetized neutron stars with emissions powered by the dissipation of magnetic energy. Here we report the detection of a long (380 s) giant flare from SGR 1806-20, which was much more luminous than any previous transient event observed in our Galaxy. (In the first 0.2 s, the flare released as much energy as the Sun radiates in a quarter of a million years.) Its power can be explained by a catastrophic instability involving global crust failure and magnetic reconnection on a magnetar, with possible large-scale untwisting of magnetic field lines outside the star. From a great distance this event would appear to be a short-duration, hard-spectrum cosmic gamma-ray burst. At least a significant fraction of the mysterious short-duration gamma-ray bursts may therefore come from extragalactic magnetars.