It didn’t just happen. Volta had experimented with static
electricity for years. In 1775 he had improved and named the electrophorus.
In 1775 and 1776 he studied methane from marshes and found that an
electric spark could ignite it.
Volta demonstrated a remotely operated pistol, using a
Leyden jar in Como to fire a pistol at the other end of a copper
wire in Milan, fifty kilometers away.
Napoleon made Volta a count in 1810.
See also in The book of science:
You can read more about Volta and Galvani, and about how to
make electric batteries: