It is all due to Maya. Maya is Supreme and it can control anyone like a magician controls puppets.
None can overcome Maya. For some time, someone can well play pretext and cleverness and get rid of Maya, but when Time takes its sway then Maya befalls over them and all their artifices gets discovered.
Why do Devas and Rishis, knower of all righteousness and realised of Brahma, act contrary to what they should- is well explained by Vyaasa to Janamejaya.
Aham-Kaara: I-ness
Devi Puraanam, Book 4, Chapter 7
मायागुणैस्त्रिभिः सर्वं रचितं स्थिरजङ्गमम् ॥ ३६ ॥
सतृणस्तम्बपर्यन्तं का तत्र परिदेवना ।
ब्रह्मा विष्णुस्तथा रुद्रस्ते चाहङ्कारमोहिताः ॥ ३७ ॥
भ्रमन्त्यस्मिन्महागाधे संसारे नृपसत्तम ।
वसिष्ठनारदाद्याश्च मुनयो ज्ञानिनः परम् ॥ ३८ ॥
तेऽभिभूताः संसरन्ति संसारेऽस्मिन्पुनः पुनः ।
न कोऽप्यस्ति नृपश्रेष्ठ त्रिषु लोकेषु देहभृत् ॥ ३९ ॥
एभिर्मायागुणैर्मुक्तः शान्त आत्मसुखे स्थितः ।
कामक्रोधौ तथा लोभो मोहोऽहङ्कारसम्भवः ॥ ४० ॥
न मुञ्चन्ति नरं सर्वं देहवन्तं नृपोत्तम ।
O Janamejaya! All this, moving and unmoving world, including even a blade of grass, are fashioned out of the three qualities of Maya. So what repentance be here?
O Best of Kings! Brahma, Visnu and Rudra, even These are all rolling in this vast ocean of Sansaara(illusionary world), being bewildered and fascinated by Aham-Kaara(the I-ness, due to which we identify ourselves as "I", as in "I" am Brahma, "I" am Vishnu, "I" am Shiva, "I" am male, "I" am young, "I" am old, "I" am.)
The great sages like Vashishtha, Narada and the other Munis are frequently taking Their births in this Sansaara.
In these three worlds, there is not even a single embodied(who has a body and form) soul, who is entirely free from this Maya, and has become calm to be immersed in the high bliss of the Supreme Self.
O Best of Kings! lust, anger, avarice, and fascination, all, arise from Aham-Kaara. These do not leave any embodied person.
Why do Gods have lust, anger, avarice, jealousy et cetera?
Devi Puraanam, Book 4, Chapter 12
हरिर्ब्रह्मा शचीकान्तस्तथान्ये सुरसत्तमाः ।
सर्वे छलविधौ दक्षा मनुष्याणाञ्च का कथा ॥ १० ॥
कामक्रोधाभिसन्तप्ता लोभोपहतचेतसः ।
छले दक्षाः सुराः सर्वे मुनयश्च तपोधनाः ॥ ११ ॥
वसिष्ठो वामदेवश्च विश्वामित्रो गुरुस्तथा ।
एते पापरताः कात्र गतिर्धर्मस्य मानद ॥ १२ ॥
इन्द्रोऽग्निश्चन्द्रमा वेधाः परदाराभिलम्पटाः ।
आर्यत्वं भुवनेष्वेषु स्थितं कुत्र मुने वद ॥ १३ ॥
वचनं कस्य मन्तव्यमुपदेशधियानघ ।
सर्वे लोभाभिभूतास्ते देवाश्च मुनयस्तदा ॥ १४ ॥
Janamejaya asks Vyaasa-
O Vyaasa, Giver of Honour! when Bhagavaan Hari, Brahma, Indra and the other excellent Devas are so dexterous in fraud and deceit, then what to speak of mortal men.
When all the Devas and all the great ascetic Munis, are burnt by lust and anger, and all Their intelligence is destroyed by avarice and greed, and They all are well diligent in fraud and trickery.
When Vashishtha, Vamadeva, Visvamitra, Brihaspati and others are addicted to vices and sinful acts; then what fate, alas! can be expected of Dharma here.
Alas! lndra, Agni, Moon, and Brahma, when These become lustful to enjoy others’ wives; then where is the goodness and virtuous behaviour in this whole existence, O Vyaasa Muni! tell me.
O Sinless Vyaasa! to whom, then, can we look upon as our spiritual guide and our advice and law givers, when all the Devas and Munis are corrupt with avarice?
Vyaasa answers-
व्यास उवाच।
किं विष्णुः किं शिवो ब्रह्मा मघवा किं बृहस्पतिः ।
देहवान् प्रभवत्येव विकारैः संयुतस्तदा ॥ १५ ॥
रागी विष्णुः शिवो रागी ब्रह्मापि रागसंयुतः।
रागवान्किमकृत्यं वै न करोति नराधिप।
रागवानपि चातुर्याद्विदेह इव लक्ष्यते ॥ १६ ॥
सम्प्राप्ते संकटे सोऽपि गुणैः सम्बाध्यते किल ।
Vyaasa answers Janamejaya-
Be he Vishnu, be he Shiva, be he Brahma, Indra or Brihaspati- all the embodied(those having a body and a form) persons are destined to have the Vikaaras- Aham-Kaara, avarice, lust, envy and anger.
O King of men! Shiva, be him Vishnu, or Brahma are attached to sensual pleasures and what sinful act can a person attached to sensual pleasures not do?
A person attached to sensual pleasures, by his cleverness, pretends to be free of these attachments of body.
But when time of difficulty arises, he also gets again caught by the bondages of body.
परोपदेशे विस्पष्टं शिष्टाः सर्वे भवन्ति च ॥ १९ ॥
विप्लुतिर्ह्यविशेषेण स्वकार्ये समुपस्थिते ।
कामः क्रोधस्तथा लोभद्रोहाहङ्कारमत्सराः ॥ २० ॥
देहवान्कः परित्यक्तुमीशो भवति तान्पुनः ।
संसारोऽयं महाराज सदैवैवंविधः स्मृतः ॥ २१ ॥
In giving instructions and doctrines to others, all the wise personalities are very clear and diligent. But when problem arises to them in their work, they easily forget all their doctrines.
One who has got body and form, how can he give up anger, lust, avarice, greed, hatred, egotism, and envy?
O Great King! thus this world is declared to be like this only.
तस्मात्कामादिकान्भावान्देहवान्प्रतिपद्यते ॥ ३० ॥
नात्र ते विस्मयः कार्यः कदाचिदपि पार्थिव ।
संसारोऽयं तु सन्दिग्धः कामक्रोधादिभिर्नृप ॥ ३१ ॥
दुर्लभस्तद्विनिर्मुक्तः पुरुषः परमार्थवित् ।
यो बिभेतीह संसारे स दारान्न करोत्यपि ॥ ३२ ॥
विमुक्तः सर्वसङ्गेभ्यो विचरत्यविशङ्कितः ।
तस्माद्बृहस्पतेर्भार्या शशिना लम्भिता पुनः ॥ ३३ ॥
गुरूणा लम्भिता भार्या तथा भ्रातुर्यवीयसः ।
And thus, an embodied being acquires these lust et cetera, O King of men! there is no doubt to be done by You.
O King! this world is pierced by lust, anger et cetera, and very rare is there some man who is free from these qualities.
And therefore one who is afraid of this world(as world has such bad and detestable qualities) never marries, and free from all attachments roams fearlessly in the world.
This embodiment is the reason why the wife of Brihaspati was abducted by Chandra, and Brihaspati himself conducted courtings with the wife of His younger brother.
How Maya has deluded the great personalities
In Devi Bhagawatam, Book 4, Chapter 20, Vyaasa tells Janamejaya about how various Gods were deluded by Maya.
Devi Puraanam, Book 4, Chapter 20
किं चित्रं नृप देवी सा ब्रह्मविष्णुसुरानपि ।
नर्तयत्यनिशं माया त्रिगुणानपरान्किमु ॥ ४ ॥
What wondrous is that Devi Maya Who makes Brahma, Vishnu, and other Devas dance incessantly, so what are men who are composed of three Gunas Sattva, Rajas and Tamas.
विद्याविद्येति तस्या द्वे रूपे जानीहि पार्थिव ॥ १० ॥
विद्यया मुच्यते जन्तुर्बध्यतेऽविद्यया पुनः ।
ब्रह्मा विष्णुश्च रुद्रश्च सर्वे तस्या वशानुगाः ॥ ११ ॥
अवताराः सर्व एव यन्त्रिता इव दामभिः ।
Her two forms- Vidya frees from these bondages, and Avidya attaches one to these bondages.
Brahma, Vishnu, Shiva and all are under Her control, and all Avataaras are controlled by Her like puppets controlled by strings.
मोहजालावृतो जन्तुर्मुच्यते न कदाचन ।
मायायां विद्यमानायां मोहजालं न नश्यति ॥ २७ ॥
उत्पित्सुकाल उत्पत्तिः सर्वेषां नृप जायते ।
तथैव नाशः कल्पान्ते ब्रह्मादीनां यथाक्रमम् ॥ २८ ॥
निमित्तं यस्तु यन्नाशे स घातयति तं नृप ।
नान्यथा तद्भवेन्नूनं विधिना निर्मितं तु यत् ॥ २९ ॥
जन्ममृत्युजराव्याधिदुःखं वा सुखमेव वा ।
तत्तथैव भवेत्कामं नान्यथेह विनिर्णयः ॥ ३० ॥
Sun and Moon
सर्वेषां सुखदौ देवौ प्रत्यक्षौ शशिभास्करौ ।
न नश्यति तयोः पीडा क्वचित्तद्वैरिसम्भवा ॥ ३१ ॥
भास्करस्य सुतो मन्दः क्षयी चन्द्रः कलङ्कवान् ।
पश्य राजन् विधेः सूत्रं दुर्वारं महतामपि ॥ ३२ ॥
Never a being tangled within the web of Maya is free because when Maya exists then no web of Maya can be destroyed.
O King! at time of creation everyone takes birth, and at the end of Kalpa, Brahma and others die as per order.
When something comes as a means of death, it kills that person for sure. O King! nothing other than this ordained by creator happens.
Birth, death, old age, diseases, sorrow or happiness are ordained to occur for sure, and no one can change them.
See the two clearly visible Devas, Sun and Moon, Their pain caused by Rahu-Ketu cannot be destroyed, even though They(Sun and Moon) Themselves give happiness to all.
And the son of the illuminer of world(Bhaas-Kara, Sun), is Lo! Himself handicapped Shani. The Moon gets depleted continuously(waxing and waning of moon) and has a lot of spots on His face. So O King! you see this creator's destiny, not even great personalities are spared by it.
Brahma
वेदकर्ता जगत्स्रष्टा बुद्धिदस्तु चतुर्मुखः ।
सोऽपि विक्लवतां प्राप्तो दृष्ट्वा पुत्रीं सरस्वतीम् ॥ ३३ ॥
See Brahma the four faced creator of Vedas and the world, Who gives intelligence to all, when He saw His own daughter Saraswati, He was afflicted with passion.
Shiva
शिवस्यापि मृता भार्या सती दग्ध्वा कलेवरम् ।
सोऽभवद्दुःखसन्तप्तः कामार्तश्च जनार्तिहा ॥ ३४ ॥
कामाग्निदग्धदेहस्तु कालिन्द्यां पतितः शिवः ।
सापि श्यामजला जाता तन्निदाघवशान्नृप ॥ ३५ ॥
कामार्तो रममाणस्तु नग्नः सोऽपि भृगोर्वनम् ।
गतः प्राप्तोऽथ भृगुणा शप्तः कामातुरो भृशम् ॥ ३६ ॥
पतत्वद्यैव ते लिङ्गं निर्लज्जेति भृशं किल ।
पपौ चामृतवापीञ्च दानवैर्निर्मितां मुदे ॥ ३७ ॥
See this Shiva, on death of His wife Sati as She burnt Her body, He too was burnt with pain and lust, though He Himself is the destroyer of pains of people.
Then Shiva burning with fire of Kaama threw Himself in Yamuna, and the river got black in colour burning from the sorrows of Shiva.
Also, once when Shiva got infatuated with lust and naked copulated in forest of Bhrigu, He was cursed by Bhrigu.
O Shameless Shiva! let your penis fall right now- this curse Bhrigu expounded over Shiva. Then Shiva started to drink the water of AmritaVaapi, dug by Daanavas.
Indra
इन्द्रोऽपि च वृषो भूत्वा वाहनत्वं गतः क्षितौ ।
Indra too had to become a bull and serve as Vaahana of Kakutstha to take Him to Earth.
Vishnu
आद्यस्य सर्वलोकस्य विष्णोरेव विवेकिनः ॥ ३८ ॥
सर्वज्ञत्वं गतं कुत्र प्रभुशक्तिः कुतो गता ।
यद्धेममृगविज्ञानं न ज्ञातं हरिणा किल ॥ ३९ ॥
राजन् मायाबलं पश्य रामो हि काममोहितः ।
रामो विरहसन्तप्तो रुरोद भृशमातुरः ॥ ४० ॥
योऽपृच्छत्पादपान्मूढः क्व गता जनकात्मजा ।
भक्षिता वा हृता केन रुदन्नुच्चतरं ततः ॥ ४१ ॥
O! what say more! Vishnu, Who is the first of all worlds, and Who is such great knowledgeable; where did go that Vishnu's omniscience and where did His divinity go, when that Hari could not identify the golden deer(Maaricha).
O King! behold the strength of this Maya. This very Raama is now rendered bewildered with Kaama.
See this Raama, burning from bereavement from Seeta, wept absent minded. He, bereft of senses, was asking trees where Seeta was, and cried aloud who had eaten or abducted Her.
शरण्यः सर्वलोकानां रामः कमललोचनः ।
शरणं वानराणां स गतो मायाविमोहितः ॥ ४८ ॥
सहायान्वानरान्कृत्वा बबन्ध वरुणालयम् ।
जघान रावणं वीरं कुम्भकर्णं महोदरम् ॥ ४९ ॥
आनीय च ततः सीतां रामो दिव्यमकारयत् ।
सर्वज्ञोऽपि हृतां मत्वा रावणेन दुरात्मना ॥ ५० ॥
That lotus eyed Raama, Who is refuge of all the worlds is Lo! now that Raama is taking refuge of Monkeys, under the influence Maya.
With help of monkeys, He created bridge over the sea. Then He killed Raavana and the big bellied KumbhaKarna, who are very great warriors.
He divinely brought Seeta back. And, after having all these many troubles to bring Her back, Raama thought Seeta was polluted by evil Raavana and abandoned Her, even though Raama is knower of everything.
किं ब्रवीमि महाराज योगमायाबलं महत् ।
यया विश्वमिदं सर्वं भ्रामितं भ्रमते किल ॥ ५१ ॥
So O Great King Janamejaya! what do I say of the extreme power of this YogaMaya, by Whom this whole world is bewildered, and rotates on and on.
So Maya is everything. Avidya Maya conceals the reality of Brahma and Vidya Maya illumines this Brahma to us. None can surpass Her as She is so powerful and is playing with this world like a puppet tied with strings.