A mantra when obtained from a competent Guru purifies one's mind eventually leading to moksha. How does that work? Prolonged meditation leads to accumulation of spiritual energy. A Guru with such a storehouse of energy transfers a portion of his energy when initiating a disciple with a mantra. When a disciple repeats such a mantra his mind is purified by that energy transferred by the Guru.
Among the Vedic sacred formulas, the Gayatri has been prescribed for
repetition from ancient times. The Vedic people had great faith in the
efficacy of mantras. The Mimamsakas considered mantras to be the
embodiments of deities. In fact, they accepted no deities other than
the mantras at all. To them Brahman is nothing but sound (shabda), and
sound produces form. Thus, the name embodied in the mantra is more
real than the form of the deity. ....
Forms are made up of nothing but
fine vibrations, and vibrations are produced by sound only, so these
ancient conceptions are tenable. From another point of view, a mantra
gains spiritual potency through many years of being associated with
holy men who have repeated it and attained realization through it. The
concept of the mantra is based on the psychological fact that much of
our thinking depends on auditory symbols. Repetition of the mantra
creates a chain of thought which induces the mind with the thought of
God, and this is the aim of all spiritual practices.
Every Vedic mantra has a rishi who first intuited it, a particular
metre in which it is composed, and a deity to whom it is addressed. In
Tantric mantras the most important element is the vija, a sacred
syllable considered to be charged with spiritual potency, as also
Shakti or power and a kilaka, a pillar (inner syllables on which the
mantra rests.) All the letters of the Sanskrit alphabets are
considered as different matrikas, minor deities around the main deity.
The mantra is charged with a special potency, so much so that the
Tantrikas believe that when offerings are made to the deity with the
appropriate mantras, the deity accepts them immediately.
Chandogya Upanishad I.1.10 says about the efficacy of the repetition
of Om: Both perform spiritual practices - he who knows and he who does
not know. But knowledge and ignorance are different in their effects.
Whatever is performed with knowledge, faith and meditation, becomes
Meditation and other spiritual disciplines by Swami Swahananda