Localized magnetic states in two-dimensional semiconductors