I was looking at the calculation of the energy stored in the capacitor, and I don't see why the calculations make sense. It goes as follows: the potential difference and charge on a capacitor satisfy the equation $$V=\dfrac{Q}{C}$$ where $C$ is the capacitance. Suppose a charge $dQ$ is taken from one plate to the other. The work done to transfer the charge is $$dU = VdQ$$ which implies that $$dU = \dfrac{Q}{C}dQ$$ and then the integral is taken from $0$ to the final charge $Q$(source: Feynman Lectures).
Now, I know that a capacitor is not charged by taking some charge from one plate to the other. Rather, it is charged because of the electric field of a battery of the circuit in which the capacitor is connected: the electrons move from the +ve plate of the capacitor to the positive terminal of the battery, thus making it positively charged, and an equal number of electrons move to the -ve plate of the battery, thus making it negatively charged. So how is the work done calculated this way correct?