According to the Wikipedia entry for the ABO blood group system:
Anti-A antibodies are hypothesized to originate from immune response towards influenza virus, whose epitopes are similar enough to the α-D-N-galactosamine on the A glycoprotein to be able to elicit a cross-reaction. Anti-B antibodies are hypothesized to originate from antibodies produced against Gram-negative bacteria, such as E. coli, cross-reacting with the α-D-galactose on the B glycoprotein.
The cited reference is Van Oss, CJ (2004) Letter to the Editor: "Natural versus Regular Antibodies The Protein Journal 23:357, available here. This source contains this statement:
..."we have known for more than four decades that these bloodgroup (antibodies) arise out of minor infections occurring very early in life..."
The cited references are:
Pettenkofer et al. Z. ImmunForsch. 119: 415-429
Springer, GF (1960) Klin. Wschr. 38: 513-514
Unfortunately I have access to neither of these articles, and besides they are presumably written in German.