I would like to preface my answer by addressing some misconceptions here. These misconceptions arise from inaccurate or inauthentic understanding of the role of yajna.
The concept and practice of yajna has been a central part of the rishi culture since the beginning. The mantras of the Rig Veda samhita are filled with actions of offering raw and cooked food. The raw food offering is called havis and the cooked food offering is typically puroDAsha. If the rishis themselves were not performing yajna, then the use of the word havis in this mantra would not make any sense:
RV 3.26.7: "ajasro gharmo havirasmi nAma" - This mantra of rishi Vishvamitra is a declaration of his enlightenment and self-realization when he declares that he is Agni (exactly equivalent to "aham brahmAsmi" "I am Brahman" of Brhadaranyaka).
"agnirasmi janmanA jAtavedA ghRtam me cakShuramRtam ma Asan|
arkastridhAtU rajaso vimAno ajasro gharmo havirasmi nAma||"
The rishi is expressing his cosmic vision by saying that he is both energy (ajasro gharmah) and matter (havis). However note that even this highly metaphysical or spiritual truth is expressed in the language of yajna.
The original meaning of yajna was highly spiritual and metaphysical. The rishis did not perform yajnas as a means to gaining material benefits. The yajna was originally meant to be an imitation of the primeval creation of the universe.
As RV 10.90.16 says, "यज्ञेन यज्ञमयजन्त देवाः तानि धर्माणि प्रथमान्यासन्" - "The Devas performed the yajna through the yajna, this was the first act (of the universe)".
In Vedic metaphysics, the primordial state is of non-existence. “Darkness covered darkness… everything was unfathomable ‘water’…. that One breathed without breath by its very nature…” (RV 10.129). The first ray of existence emerged when Agni was kindled. This concept is described as an allegorical story in RV 10.51 and 10.124 where the Devas are pleading with Agni to come out of the darkness so that the yajna can begin. Then once the yajna has begun, what can be offered into it? What else is there except Agni himself? This is why it is frequently said, “Agni - offer yourself to yourself, or offer to your own house, or grow your own body”. How can this be? According to Vishvamitra (RV 3.26.7) Agni is “both matter and energy”, so the grains that are offered are already Agni’s form, so basically we are adding Agni to Agni. The same event can be found in Purusha Suktam (RV 10.90) where Purusha is the only existing entity so he is himself offered into the yajna, and the outcome is his divisions become the parts of the universe. So in summary, the first yajna was the act of creating the universe. As an act of reverence to this mystical creation, the yajna is performed by human beings, as a way to emulate the gods. Each day, each fortnight, each month, each season, each year are all mini-creations. So by performing a yajna at each new time division, we are emulating the macro-creation and immersing ourselves in the metaphysical truth of one existence, consciousness and bliss.
So you see, originally yajna had the role of a meditation accessory. Like rosary beads, or pictures or idols. Performing the yajna was like enacting a play, only this play is meant to help us understand and meditate (manana, nididhyAsana) on the metaphysical truth behind existence.
Now, as with any other spiritual activity, when it spills out into the general society, common people will adapt it to suit their needs at their own level. This is seen in temple worship as well, where the saint is just involved in pure devotion, whereas common folks are involved in elaborate pujas and rituals to gain some material benefit.
As with temple rituals, where an original prototype is replicated and expanded during later use, in the case of later elaboration of yajnas, original mantras which seemed to be in the context of the yajna were attached to the specific ritual.
So, obviously, in a yajna for Indra, the Indra mantras would be used, in an Ashvina sattra, Ashvin mantras would be used, in a Soma yajna, Soma mantras would be used, and so on.
The rishis composed mantras to various deities when worshiping them. That is the main thing to understand. The mantras and the yajnas are not separate things that were artificially clubbed together. The mantra is the speech and the yajna is the action, both employed together to meditate on the deity. So the rishis set the precedent by composing mantras for Indra when worshiping Indra, and so on for other deities and occasions.
Non-rishis or laypeople followed what the rishis did, and continued to use those mantras for those purposes. To an extent, this would be obvious. For example, Agni mantras would not be used for Indra or Varuna. The usage needs to make sense for the context, and the rishis showed the context. This grew and expanded over centuries and then we had tradition, which just solidified those centuries of usage.
As an example, one of the main hymns used in the Ashvamedha yajna is the one by rishi Dirghatamas (RV 1.163) which is in praise of the horse. The Ashvalayana Shrauta Sutra prescribes the recitation of the first 11 mantras during the preparation ritual of the horse. Ashvalayana Shrauta Sutra 10.8:
तमवस्थितमुपाकरणाय यदक्रन्द इत्येकादशभिः स्तौत्यप्रणुवन्
To prepare the anointed horse, he is worshiped using the first eleven ṛks of the hymn beginning with 'yadakranda' (RV 1.163)
Now, the hymn may been composed by Dirghatamas for the occasion of the Ashvamedha, or he may have had a vision of deep truth and described it using the symbol of a horse. Similar cosmic metaphysical idea of the horse is also seen in Brhadaranyaka Upanishad.
Ultimately, there may not be an entirely systemically logical reason for specific mantras to be associated with specific rituals. There are hundreds of mantras to choose from, and here tradition and custom play a more defining role.