The reason why "por" is not used in:
- Llevo estudiando español dos años.
is that "dos años" is the direct object of the verb "llevar" (take), which can be considered more or less equivalent to "ocupar" (occupy, take up) and "pasar" (spend). The sentence can in fact be restated as follows:
Llevo dos años estudiando español. (Literal, non-idiomatic translation: I have taken two years studying Spanish.) (*)
He ocupado dos años estudiando español. (Literal, non-idiomatic translation: I have taken up two years studying Spanish.)
He pasado dos años estudiando español. (Literal translation: I have spent two years studying Spanish.)
The particular feature of the direct object of these verbs is that it expresses a period of time, and can thus be confused with an adverb of quantity (¿Cuánto llevas estudiando español? -> Dos años.) The fact that "dos años" (or any other period of time) is required to complete the sense of the sentence makes it a direct object. Proof of this is that this acceptation of the verb appears as transitive in DLE:
Entry 15 for verb "llevar":
- tr. Haber pasado un determinado período de tiempo en una misma situación o en un mismo lugar. Llevaba seis años de carrera. Lleva tres meses enfermo. Llevamos aquí muchos días.
Entry 16 for verb "pasar":
- tr. Estar durante un tiempo determinado en un lugar o en una situación. Pasan los veranos en la playa. Ha pasado la noche a la intemperie. Pasó años sin hablarme.
In conclusion, "por" can only be optionally used to introduce a period of time with intransitive verbs, or with transitive verbs having a direct object other than the period of time:
He estudiado español (por) dos años. (Here, "español" is the direct object.)
He estudiado (por) dos años. (Here, "estudiar" is intransitive.)
(*) It might also call your attention that, while "llevar" is used in the present to express an action starting in the past and continuing into the present, with "pasar" we use the present perfect to express the same meaning. The reason for this is mostly idiomatic.