Answer:
The female produces the eggs in the first place, of course. She transfers them to the male’s pouch, he fertilizes them during the transfer, and the weird male pregnancy thing then proceeds from there.
As for why: this is probably because the system enhances the reproductive success of both male and female. It’s energetically expensive to produce eggs. It’s also expensive to gestate fetuses. In seahorses, the male takes on the second part of this energetic load. Given that she is relieved of this burden, the female remains in better physical shape and can produce more eggs. Incidentally, when any parental care is provided in fish, it’s generally the father who provides that care. Seahorses take it to extremes, but so do mouthbrooding fish, where one parent — in most mouthbrooding species, the male — collects the fertilized eggs in his mouth and holds them there, guarding them against predation, until they hatch. He has to fast for the whole period until they hatch, so you can see why the male seahorse’s pouch is far preferable.
One mammalian system that is roughly similar in terms of sharing the energy costs of raising the young is that seen in marmosets and tamarins. Of course the female cannot transfer the young to the male prior to birth, but as soon as the infants are born, she hands them to the male(s) in the group. Thus, the male(s) take on the burden of carrying the infants — which is a big, energetically expensive burden as they grow — allowing the female to put her energy into lactation. This allows marmosets and tamarins to raise twins every year, rather than just one baby per year or less like other primates.