In general, the mass of a system of particles (e.g. the proton which is a system of three quarks) is not the sum of the masses of the constituent particles.
The total energy of the system equals the sum of (a) the rest-energies E0 of the constituent particles (which correspond to their individual masses via E0 = mc2), (b) their kinetic energies, and (c) the potential energy of the system. The total energy corresponds to the mass of the system via E = mc2, if the system "as a whole" is at rest, i.e. if the total (vector) momentum is zero.
(By "mass" I always mean what is often called "rest mass.")