You are right. This phenomenon goes all the way back to Vulgar Latin and applies to other Romance languages, as well.
First of all, a little phonetics background: the vowels /e/ and /i/ are what phoneticians call front vowels, because they are articulated in the frontal part of the mouth, unlike, for example, /a/, /o/ and /u/, which are articulated more to the back, closer to the throat. Actually, /a/ is a front vowel, but articulated at a lower part of the mouth. And the consonants /k/ and /g/ are velar consonants, that is, they are articulated with the back of the tongue against the back part of the roof of the mouth (velum).
Now, take the Latin word CIRCA (/kirka/), for example. To pronounce this word, the speaker has, first, to articulate a consonant at the back of the mouth (the velar /k/) followed by a vowel at the front (the close front vowel /i/). In cases like this, it is very common the occurrence of a phonological phenomenon called palatalization, which played a key role in the development of the Romance languages. Palatalization involves displacing the point of articulation to an area closer to the hard palate (the middle part of the roof of the mouth).
In this specific example, the consonant /k/ became an affricate: /tsirca/. Later on, the affricate /ts/ lost is occlusive articulation (the /t/ part) and became /sirca/ (modern Spanish "cerca"). A similar thing happened to /g/ before /e/ and /i/ (e.g. Latin /gens/ > Spanish /xente/). In the case of /a/, /o/ and /u/, due to their being back and/or lower, they did not cause palatalization. So, for example, Latin CASUS became Spanish "caso" (and not */tsaso/ or something like that).
But note that, although these sounds underwent a series of important changes, their spelling was kept similar to the original Latin spelling, for some reason. In order to avoid ambiguity, a special spelling convention was then adopted to represent the sequences of sounds /ke/ and /ki/: "que" and "qui", respectively.
Que
,Quien
,Guerra
,Cigarra
. But there is a rule for the orthography./k/
,/s/
, or/θ/
; while "q" is a letter that cannot stand alone but only part of the digraph "qu" where it also represents the sound/k/
. Which letter to use for which sound depends on the rules of orthography (spelling).