We study the multiple-pulse spin locking dynamics of the nuclear spins in a liquid or gas entrapped in nanosized cavities. Two cases are considered with the cavities being either in orientational order or isotropically disordered. The spins inside the cavities are coupled by dipole-dipole interactions with the same interaction constant. It is shown that, under the high temperature approximation in a spin system irradiated by a multiple-pulse sequence, the quasi-equilibrium state is established. An analytical expression is obtained describing the dependence of the steady-state magnetization on the structural parameters of a nanocavity and the characteristics of a gas or liquid confined in nanocavities. The relaxation process which follows the establishment of the equilibrium is considered. For the case of the orientationally ordered cavities, the analytical expression for the relaxation time is derived. When the nanocavities are isotropically disordered, the time dependence of the magnetization is numerically calculated. As shown for this case, the relaxation process is characterized by two time constants differing by two orders of magnitude. An advantage of the application of the multiple-pulse spin locking measurement method over the NMR cryoporometry technique is that the measurements of magnetization and its relaxation, along with the information about the cavity size, allow determination of the shape and orientation of the nanocavity.