Herman Melville’s assertion on the reverse magnetization of a compass is entirely plausible.
Lightning involves very high currents with high associated magnetic fields.
They remagnetize exposed outcrops of high-coercivity, high resistance to the effect of an applied magnetic field, rocks with ease. Currents exceeding 10,000 amperes have been deduced from rock magnetizations.
The associated magnetic fields could easily demagnetize or reversely magnetize a compass needle.
A moving electrical field will induce a magnetic field, and an electrical discharge such as lightning can easily cause a compass needle to lose or reverse its magnetization.
What the questioner failed to mention is that Captain Ahab fashioned a new compass by striking a sailmaker’s needle and thereby magnetizing it.
This phenomenon is possible and has happened. It has been demonstrated more than once by accidentally dropping a pair of expensive tweezers used for handling metallurgical samples. The fall was sufficient to magnetize the tweezers.
Ferromagnetic materials are composed of microscopic magnetic domains, which may be oriented in random directions producing a demagnetized state.
By aligning the domains in more or less the same direction, the material becomes magnetized.
In some cases, a sharp blow will impart enough energy to make this happen.