Protons and neutrons are both subatomic particles known as nucleons, and they are found in the nucleus of an atom. While protons have a positive charge and neutrons have no electric charge, they have very similar masses.
The reason for the similar masses of protons and neutrons is due to the strong nuclear force that binds them together within the nucleus. The strong nuclear force is one of the fundamental forces of nature and is responsible for holding the nucleus together despite the repulsive electromagnetic forces between positively charged protons.
The strong nuclear force acts at very short distances, within the nucleus, and is much stronger than the electromagnetic force over these distances. It overcomes the electrostatic repulsion between protons and helps to bind them together. This force is mediated by particles called mesons, specifically the exchange of pions.
The masses of protons and neutrons arise from the interactions between quarks, which are elementary particles that make up protons and neutrons. Protons are composed of two up quarks and one down quark, while neutrons consist of one up quark and two down quarks. The masses of the individual quarks are relatively small, but the binding energy and interactions among them contribute to the overall mass of protons and neutrons.
Although protons and neutrons have different compositions and arrangements of quarks, the strong nuclear force is responsible for their comparable masses. The precise mechanisms of these interactions are complex and involve the dynamics of quantum chromodynamics (QCD), which is the theory that describes the strong nuclear force.