Very recently, two new two-dimensional (2D) layered semi-conducting materials MoSi2N4 and WSi2N4 were successfully synthesized in experiments, and a large family of these two 2D materials, namely MA2Z4, was also predicted theoretically (Science, 369, 670 (2020)). Motivated by this exciting family, in this work, we systematically investigate the mechanical, electronic and optical properties of monolayer and bilayer MoSi2P4 and MoSi2As4 by using the first-principles calculation method. Numerical results indicate that both monolayer and bilayer MoSi2Z4 (Z = P, As) present good structural stability, isotropic mechanical parameters, moderate bandgap, favorable carrier mobilities, remarkable optical absorption, superior photon responsivity and external quantum efficiency. Especially, due to the wave-functions of band edges dominated by d orbital of the middle-layer Mo atoms are screened effectively, the bandgap and optical absorption hardly depend on the number of layers, providing an added convenience in the experimental fabrication of few-layer MoSi2Z4-based electronic and optoelectronic devices. We also build a monolayer MoSi2Z4-based 2D optoelectronic device, and quantitatively evaluate the photocurrent as a function of energy and polarization angle of the incident light. Our investigation verifies the excellent performance of a few-layer MoSi2Z4 and expands their potential application in nanoscale electronic and optoelectronic devices.