Atomically precise metal nanoclusters (NCs) with unique optical properties and abundant catalytic sites are promising in photocatalysis. However, their light-induced instability and the difficulty of utilizing the photogenerated carriers for photocatalysis pose significant challenges. Here, MAg24 (M=Ag, Pd, Pt, and Au) NCs doped with diverse single heteroatoms have been encapsulated in a metal-organic framework (MOF), UiO-66-NH2, affording MAg24@UiO-66-NH2. Strikingly, compared with Ag25@UiO-66-NH2, the MAg24@UiO-66-NH2 doped with heteroatom exhibits much enhanced activity in photocatalytic hydrogen production, among which AuAg24@UiO-66-NH2 presents the best activity up to 3.6 mmol g-1 h-1, far superior to all other counterparts. Moreover, they display excellent photocatalytic recyclability and stability. X-ray photoelectron spectroscopy and ultrafast transient absorption spectroscopy demonstrate that MAg24 NCs encapsulated into the MOF create a favorable charge transfer pathway, similar to a Z-scheme heterojunction, when exposed to visible light. This promotes charge separation, along with optimized Ag electronic state, which are responsible for the superior activity in photocatalytic hydrogen production.