I believe the original saying goes back to Shakespeare and I believe the term "petard" got applied to that type of animal trap only in the 19th century. But that's a quibble.
Hamlet, act III, scene 4, lines 206 and 207: "For 'tis sport to have the engineer Hoist with his own petard."
Which certainly supports the bomb explanation of the phrase!