Latin mens, mentis produced ablative mente
This practice began all the way back in Classical Latin, passed into Vulgar Latin and Proto-Romance and thence to all modern Western Romance tongues: Italian, Spanish, Portuguese, Catalan, Occitan, French, and the many related neighboring languages in that group.
The Spanish feminine substantive la mente derives from the same ultimate Latin origin as does its -mente suffix used to derive adverbs from adjectives: mens/mentis, a feminine noun from the third declension. This noun meant thought or mind, the latter being its English cognate through a common Proto-Indo-European ancestor.
The Wiktionary entry for Latin mens observes:
In most classical Latin, the ablative singular mente was used with a feminine adjective to form a phrasal adverb that expressed a person's state of mind, such as vēlōcī mente (“quick-mindedly, with a quick mind”)
- 29-19 BCE, Virgil, Aenid, book 4, line 105:
sensit enim simulata mente locutam
for she realized that (she) had spoken with false purpose.
In Late Latin, this construction began to be extended to other adjectives and uses as well, and in Vulgar Latin and the later Romance languages, it became a general adverbial suffix.
Latin’s ablative case had quite a few uses that we now represent with a separate preposition instead of nominal case inflection. One of these prepositions used to translate the ablative case is with, so using mens in the ablative mente meant “with (a/the) mind/thought”.
By adding an adjective to that noun, as in rapida mente, it therefore indicates in a rapid manner. You can see why the adjective has to agree with a feminine noun: because mens itself was feminine.
This is also why you can chain together adverbs in Spanish by using a sequence of feminine adjectives saving up the -mente adverbial suffix for the last adjective in the series:
Se lo explicó lenta, clara y cariñosamente.
That -mente suffix on the last term, cariñosa-mente, distributes to both the earlier terms lenta and clara. But you do not use suspension hyphens in Spanish for this as one might do in English.
Body and Mind
This Romance habit of using a version of “mind” to derive adverbs from directly in lieu of using a longer phrase corresponds to the Germanic habit of using a version of “body” to do so. We no longer think about it, but the English -ly suffix comes from a word that meant “body”, lich. This practice also occurs in related languages like Dutch and German.
So where Romance tongues use a “mind” suffix, Germanic ones use a “body” suffix for the same purpose.