The electric potential energy is the potential energy of a system of charges. We know that the potential energy is always a shared property of two charges. An isolated electric charge has its field around it and this field exerts force on other charges in its influence, and that force is what causes the electric potential energy due to the mutual interaction of the charges.
Electric and gravitational forces are both conservative forces that is, the total mechanical energy of a system is conserved. For example, when you throw a body upward, the body reaches its maximum height and gains maximum potential energy and falls back converting that potential energy back into kinetic energy and the total mechanical energy (kinetic energy plus potential energy) at each point on its path is constant or conserved.
In Figure 1 a test charge \(q_0\) moves in the direction of force which means the force and the displacement are in the same direction and therefore the work done by the electric field \(\vec{E}\) is positive.
The electric force on the test charge \(q_0\) is \(\vec F=q_0\vec E\) which is similar to the case for the gravitational force \(\vec F=m\vec g\). Since the work done is positive and \(y_1 > y_2\) the work done along a displacement from \(y_1\) to \(y_2\) is
\[W = {q_0}E{y_1} - {q_0}E{y_2} = q_0 E(y_1 - y_2) = -q_0 E(y_2 -y_1)\]
You can also define work done as the initial potential energy minus the final potential energy which is the same as the negative of the change in potential energy. This is a bit confusing but you should note that work done is positive if the force and displacement both are in the same direction and negative if they are in the opposite direction.
We consider a system of a test charge \(q_0\) and point charge \(q\) as shown in Figure 2. Now consider that the charge \(q_0\) moves from position \(r_a\) to \(r_b\). The work done on the charge by the electric field \(\vec E\) through a small displacement \(dr=dl\cos \alpha \) in the direction of electric field is
\[dW = \vec F.d\vec l = {q_0}\vec E.d\vec l = {q_0}Edl\cos \alpha = {q_0}Edr\]
The total work done by the electric field is calculated by the integration of the small work done \(dW\) from \(r_a\) to \(r_b\).
\[\begin{align*} W &= \int_{{r_a}}^{{r_b}} {dW} = {q_0}\int_{{r_a}}^{{r_b}} {Edr} \\ &= kq{q_0}\int_{{r_a}}^{{r_b}} {\frac{1}{{{r^2}}}dr} = kq{q_0}\left( {\frac{1}{{{r_a}}} - \frac{1}{{{r_b}}}} \right)\\ {\rm{or,}}\quad W &= \frac{{kq{q_0}}}{{{r_a}}} - \frac{{kq{q_0}}}{{{r_b}}} \tag{1} \label{1} \end{align*}\]
The work done is independent of the nature of path but depends only on the initial position \(\vec r_a\) and the final position \(\vec r_b\). If the charge moves from a position to anywhere and comes back to the same position, the work done is zero. The work done is the initial potential energy \(U_1\) minus the final potential energy \(U_2\) (that is, the negative of the change in potential energy). So, comparing \(W=U_1 - U_2\) and Eq.\eqref{1}, the potential energy of the charge \(q_0\) and \(q\) at a distance \(r\) is
\[U = k\frac{{q{q_0}}}{r} \tag{2} \label{2}\]
Note that potential energy is the shared property of the system. So we don't say the potential energy of a specific charge but we say the potential energy of the system of both charges. The potential energy of the system of \(q\) and \(q_0\) at infinite separation is zero. And the work done is the initial potential energy at a distance \(r\) minus the final potential energy.
\[W=\frac{q{{q}_{0}}}{4\pi {{\epsilon }_{0}}r}-0\]
From this expression you can understand that the work is done along a displacement from the initial position at \(r\) to the final position at infinity. If the charges have the same sign, we can interpret the potential energy at a distance \(r\) as the total work done by an external force to bring the test charge \(q_0\) from infinity (position of lower potential energy) to the position at the distance \(r\) (position of higher potential energy) against the electric force. If the charges \(q\) and \(q_0\) have opposite sign, the potential energy can be interpreted as the amount of work done required to push the charge from the position at the distance \(r\) (position of lower potential energy = more negative) to the infinity (position of higher potential energy which at infinity is zero).
The potential energy of a test charge \(q_0\) at a point \(p\) (see Figure 3) and collection of charges \(q_1\), \(q_2\), \(q_3\), \(q_4\) etc. at distance \(r_1\), \(r_2\), \(r_3\) and \(r_4\) respectively from the test charge is the algebraic sum of the potential energies due to individual charges (as Eq.\eqref{2} suggests).
\[\begin{align*} {U_{{\rm{total}}}} &= k{q_0}\left( {\frac{{{q_1}}}{{{r_1}}} + \frac{{{q_2}}}{{{r_2}}} + \frac{{{q_3}}}{{{r_3}}} + \frac{{{q_4}}}{{{r_4}}}} \right)\\ {\rm{or,}}\quad {U_{{\rm{total}}}} &= k{q_0}\sum\limits_{n = 1}^n {\frac{{{q_n}}}{{{r_n}}}} \tag{3} \label{3} \end{align*}\]
Any static charge distribution can be considered as the collection of point charges and the above equation shows us the potential energy of a test charge and a static charge distribution. So, the force exerted by the electric field of any static charge distribution is conservative.