All perfect praise be to Allaah, The Lord of the Worlds. I testify that there is none worthy of worship except Allaah, and that Muhammad, sallallaahu ‘alayhi wa sallam, is His slave and Messenger.
explicated the verse saying, “It means, those whom the disbelievers and polytheists worshipped instead of Allaah actually worship Allaah and draw closer to Him by obeying Him and doing whatever leads to His pleasure and achieving His rewards. Those who had worshipped them instead of Allaah should have followed their example!” [Adhwaa’ Al-Bayaan]
This meaning is supported by the tradition reported on the authority of Ibn Mas‘ood as he said, “This verse was revealed in relation to a group of Arabs from the tribe of Khuzaa'ah who were worshipping the Jinn; those Jinns embraced Islam but that group of Arabs continued to worship them; so, this verse was revealed. Thereupon, Allaah, The Exalted, revealed (what means): {Those whom they invoke seek means of access to their Lord.} [Quran 17:57]”
Moreover, Ibn ‘Abbaas said, “This verse was revealed in relation to those who worshipped ‘Uzayr (Ezra), ‘Eesa (Jesus) and his mother.” It has also been reported on the authority of Ibn ‘Abbaas, Ibn Mas‘ood, Ibn Zayd, and Al-Hasan
that this verse was revealed in relation to the worshippers of angels.
Allaah Knows best.