During the formation of a nucleus, the protons and neutrons come closer to a distance of 10-14m. The energy required for the purpose is spent by the nucleons at the expense of their masses. So mass of the nucleus found is less than the sum of the masses of the individual nucleons.
Since proton number and neutron number are conserved in a nuclear reaction, the total rest mass of neutrons and protons is the same on either side of a reaction. But the total binding energy of nuclei on the left side need not be the same as that on the right hand side. The difference in these binding energies appears as energy released or absorbed in a nuclear reaction.