Work is done on the oscillator by the driving force. This puts energy into the oscillator. At the same time energy is taken away as the oscillator does work against the damping force.
Initially the energy stored in the driven oscillator increases with time because the work done on it by the driving force exceeds the work done against the damping force. However the average work done by driving force increases more slowly, in proportion to the amplitude, whereas the work done against the damping force increases more quickly, in proportion to the square of velocity. In addition the driving force gradually gets out of phase with the motion of the oscillator, reaching a maximum when the velocity is low, whereas the damping force is always in phase with the velocity, reaching a maximum when the velocity is maximum.
Eventually the work done against the damping force increases enough to equal the work done by the driving force, so that the average work done on the oscillator is zero.
This is similar to an object falling through the air. Its velocity increases until it reaches terminal velocity. Then the work being done on it by gravity equals the work being taken away by air resistance. (However in this case the driving force like the damping force is always in phase with the velocity of the falling object.)
In the case of an undamped oscillator there is no resistance. However the phase difference between the driving force and the velocity of the oscillator gradually increases until it is exactly $\pi$ radians out of phase with the velocity of the oscillator. Then the average work done by it per cycle is zero. Like the restoring force, on one quarter cycle it is doing positive work, increasing the velocity and kinetic energy of the oscillator; on the next quarter cycle it is doing negative work, slowing the oscillator and taking energy away from it.