I thought this was an interesting question, so I tried to research it. There appear to be many explanations, but the most likely seems to be a first letter substitution. In the same way that my mum used to say "Oh sugar!" because she wouldn't use the "sh" word in front of us.
So it was probably be a substitute for "Mary" or "Moses" or "Mother of God" all of which are mild blasphemies now, but were once considered rather offensive. (And mackerel is such a good word, isn't it? Rolls nicely off the tongue).
I remember seeing Robin, boy wonder, saying "Holy Macaroni" in some early Batman comics, which is probably an enhancement for comedic value.