I am confused by the whole mechanism of enlightenment or self realization. If one's self is actually the universal self or brahman which is beyond maya how can it get attached to it?
Short Answer: Attachment comes because of prakriti which is its objective manifestation.
As per Trika Philosophy, Brahman manifests himself into 36 elements, the 12th and 13th elements are Purusha and Prakriti.
Siva(Brahman) through Mayasakti which limits His universal knowledge and power becomes Purusa or the individual subject. Purusa in this context means every sentient being. Purusa is also known as anu in this system. The word anu is used in the sense of limitation of the divine perfection.
Here limitation perfection, bondage is because of prakriti which consists of 3 Gunas, rajas tamas and sattva. And Prakriti further brings in manifestation of Buddhi, Ahamkara, and Manas which bring sense of bondage in an individual. The bondage of the individual is due to innate ignorance. It is this primary limiting condition which reduces the universal consciousness to an anu or a limited creature.
Whole this phenomena happen within Brahman's consciousness, hence the supreme consciousness which is cause of all this manifestation stays unaffected, however its conditioned manifestation which is Purusha stays attached to maya because of its prakriti.
Reference : Shiva Sutras, Introduction part, XXVi-XXIX
According to Yoga-Vasistha, consciousness or brahman becomes associated with mAyA (hence leading to creation) due to accidental coincidence. This is another way of saying that this happens without any apparent cause.
Below are some extracts of Yoga-Vasistha as translated by Swami Venkatesananda.
The story of Ahalya
However, mind, intellect, egotism, individualised consciousness, action, fancy, birth and death, latent tendencies, knowledge, effort, memory, the senses, nature, Maya or illusion, activity and such other words are but words without corresponding reality: the sole reality is the infinite consciousness in which these concepts are conceived to exist. All these concepts have arisen when, by accidental coincidence (the crow dislodging the cocoanut), the infinite consciousness in a moment of self-forgetfulness viewed itself as the object of perception.
The story of Ikshvaku
The notion ‘I’ arises in Brahman accidentally (like the crow alighting on the cocoanut tree and the cocoanut falling down without causal relationship).* In truth, I am Brahman, the world is Brahman, there is neither a beginning nor a ceasing.
The story of the hunter and the deer
The appearance of diverse objects in the universe arises in the infinite consciousness when it becomes aware of itself—coincidentally (like a ripe cocoanut falling when a crow alights on it). Whenever and wherever this consciousness contemplates itself in whatever manner, then and there it appears so, without any cause.
The story of the philosopher's stone
Originally, Brahman alone existed as Brahman. In it innumerable substances appeared like ripples on the surface of the ocean. This is known as nature. It is not causally related to Brahman, but it happened like a cocoanut accidentally falling when a crow happened to alight on it. In that nature are found diverse creatures endowed with diverse characteristics.
The story of King Janaka
In this world which appears to have been created, even as the fruit of cocoanut-palm might appear to have been dislodged by a crow which coincidentally happens to alight on the tree at that moment, sheer ignorance generates feelings like ‘this I should have’ and ‘this I should reject’. It is better to spend one’s time in seclusion or in hell than to live in this world-appearance