‘Abdullah b. Mas’ud (may Allah be pleased with him) reported Allah’s Messenger (ﷺ) as saying: “Envy is permitted only in two cases: A man whom Allah gives wealth, and he disposes of it rightfully, and a man to whom Allah gives knowledge which he applies and teaches it.” [Bukhari and Muslim]
According to Imam An-Nawawi’s explanation, the word Hasad (jealousy) is used here in the sense of Ghibtah, i.e. envy. In Islam, jealousy is forbidden and is held unlawful. The reason being that one who is jealous wants the one he is jealous of to be deprived of that blessing. Envy is permissible for the reason that when one sees that a person has been graced by Allah (subhana wa ta’ala) with certain gifts and qualities, he also desires to be blessed with those gifts. In the latter case, he does not grumble and grieve but eagerly prays to Allah (subhana wa ta’ala) for those gifts.
‘Knowledge’ stands for the knowledge of the Quran and Hadith because this knowledge alone is beneficial for man, and it is through this knowledge that correct judgements can be made. This hadith encourages acquiring useful knowledge and wealth to spend only in the ways pleasing to Allah (subhana wa tala).
(Riyad Us-Saliheen, Book 12: Knowledge, Chapter 241: Virtues of Knowledge which is Learnt and Taught for the sake of Allah)