How is energy stored in magnetic and electric fields?

This is a qualitative answer:

The underlying level of nature is quantum mechanical. The classical concepts, like electric and magnetic fields, rise from this underlying level.

Take a charged capacitor. All the negative charges are on one face and all the positive on the other and there exists a classical electric field between the plates which has energy. This energy is the potential energy which separates the quantum mechanical charges, electrons have been moved from one side to the other and ions were created, and the energy stored comes from this operation.

If one takes an electron by itself, there is a charge, and an electric field associated mathematically with that charge, but there is no energy to be taken or given to the single electron because the charge is intrinsic. Thus the how of ensembles which give electric fields ,ends up to the basic question at the level of a single electron getting the answer "because that is what has been observed".

This always happens with physics because it is a science that uses mathematics to fit observations, and there are basic observations that are assumed. Particle physics has codified them in standard model particle table. So one can answer this question sequentially, peeling the onion of complexity, until one reaches the basic measured facts.

A more complicated argument can be made for the magnetic field, (one has to use the concept of moving charges, an observation) and in the end the answer to the last how will again end in the standard model table.