Complementary to the experimental finding that ZnO nanoparticles become ferromagnetic when coated with N and S containing ligands such as dodecylamine and dodecanethiol [Garcia et al., Nano Lett. 7, 1489 (2007)], we provide the first theoretical understanding of the origin of magnetism in ligated ZnO nanoparticles as well as the structural properties of the ligated systems by using density functional theory and generalized gradient approximation for exchange and correlation, and a cluster model for the nanoparticles. We show that N or S atoms of the ligand bind to the Zn sites. The accompanying changes in the Zn-O bond length, hybridization between Zn 4s orbitals with N 2p or S 3p orbitals, and consequently the redistribution of charges between Zn and O atoms result in a magnetic system where the 2p electrons in O and N, and 3p electrons in S sites are spin polarized. Furthermore, the sites nearest to the Zn atom attached to the ligand carry bulk of the magnetic moment. Studies, as a function of cluster size, also illustrate that magnetism resides only on the surface. Our results confirm that the use of ligands can pave a new way for introducing magnetism in ZnO nanostructures, which can be used to develop magnetic sensors to detect N and S containing molecules.