Most Romance languages don't have /x/ (like the j in hijo), nor did Latin. Where did Spanish /x/ come from? Internal development, Arabic influence, or something else? Since Moroccan Arabic also has /x/, one would suspect Arabic influence; but perhaps that is simplistic.
Spanish did have Arabic influence but it seems that the jota (the name for the letter j in Spanish) came from Latin, going through some variations.
An hypothesis, provided in "Introducción a la historia de la lengua española" by Melvyn C. Resnick, at the page 38 (Scroll down until you read «Podemos postular...» going onwards) explains the path from ōcŭlu to ojo:
- The [k] (letter C) voices and becomes a [g]: ōcŭlu > ogulu
- The post-tonic1 vowel falls: ogulu > oglu
- There is a "shift" from the final -u to -o: oglu > oglo
- The [g] "vocalizes" (i.e. it becomes a vowel sound) and becomes the semivowel [y], called Yod letter's name from the Hebrew alphabet: oglo > oylo
- The yod and the semivowel exchange position (called metatesis in Spanish): oylo > olyo
- The group [ly] gives the [ l̺ ], similar to the Spanish "ll": olyo > ol̺o
- The [ l̺ ] becomes the semivowel [y]: ol̺o > oyo
- The semivowel [y] intensifies as the g in the French rouge, the IPA is [ž]: oyo > ožo
- Following a general tendency from the Spain of the XVIth century, the sound becomes voiceless2, the IPA is [š]3: ožo > ošo
- During the XVIIth century, in Spain, the sound [š] is lost, and the current "jota" sound with the modern pronunciation is originated. It's the same sound from the south american Spanish, the IPA is [χ]: ošo > oχo
- In the southern Spain and the northern countries of south america, the [χ] results in [h]: oxo > oho, written form ⟶ ojo.
The evolution of mūlǐēre > mujer and fīlǐa > hija follow the same pattern as ōcŭlu > ojo [...], but not all words with such features have the same result.
For now, I haven't found other clear references but there are some hints that confirm the process described above. If you want, I can find some other references to further back up this process.
I made a search about the possible causes of this "velarization", but little is there. Especially one source that could hold this information is not (fully) available online, which is "Historia de la lengua española" by Rafael Cano Aguilar. The chapter that interests us is the number 32, titles "De las sibilantes y palatales antiguas a las «ces», «eses» y «jotas» modernas", but the book stops around the chapter 29 in that preview, so it's not possible to see what is written there. (If someone can access those pages in that preview, please comment.)
Other texts I've consulted are "Variation and change in Spanish" by Ralph John Penny, "A history of the Spanish language", always by R.J. Penny, "Historical romance linguistics: retrospective and perspectives" by Randall Scott Gessa and Deborah L. Arteaga, and "Historia de la lengua española" by Rafael Lapesa, but none of these talked about the causes, although they confirmed that passage from [ʃ] to [χ].
The only place that mentions the (possible) causes is the wikipedia article about the "Adjustment of the sibilants in Spanish" 4, but I haven't found anything that confirms or denies the information being found there, although I think they are a good starting point if you want to further investigate.
For those who don't know Spanish, I'll summarize the main points of the section "Posibles causas" (possible causes):
Phonetic changes are natural processes in all languages. Although there are phonetic laws that act outside of the linguistic genealogy boundaries; [...].
When treating phonetic changes or evolution, we must take into account that there never is a single reason, rather some that "act together". Among the causes, linguists distinguish between internal and external causes, [...]. In this case, linguists believe that the loss of those voiced sounds was due to a spanish-basque bilingualism [...], while others think that it was an internal simplification due to structural causes.
[...] Taking into account the fact that the /s/ was an apico-alveolar, there wasn't much difference between that and /š/. For this reason, Spanish speakers, in order to keep the two phonemes distinguished, started to bring "back" the place of articulation, transforming the latter into a velar. [...]
Hope this helps. Judging from what I found, we know fairly surely how we got this sound "out of nowhere" but we don't know the causes for sure yet. If you don't feel satisfied, I can try to make a deeper research.
1: I think postónica refers to post-tonic.
2: "Ensordecer" literally means "to deafen", but I think here it's mean as "to devoice/to become voiceless".
3: [š] = [ʃ]
4: The original title of the article is "Reajuste de las sibilantes del idioma español".
To rephrase the first answer above: Spanish /x/ comes from earlier /ʃ/ by a process of backing. Old Spanish of the 1300's had the sounds /ʃ ṣ ts ʒ ẓ dz/ where /ṣ ẓ/ indicate apico-alveolar sibilants. By the 1400's this had become /ʃ ṣ s ʒ ẓ z/ with apico-alveolar /ṣ ẓ/ vs. lamino-dental /s z/ (similar to English). The sound /ṣ/ was quite similar to /ʃ/ on the one hand and /s/ on the other. Usually a language with tricky-to-distinguish sound pairs would eliminate one of the two (cf. Portuguese which once had the same sounds but changed ṣ > s and ẓ > z, still distinguished in spelling: intervocalic ss vs. ç, s vs. z). But in Spanish, before such a change could happen, a different change happened, with the voiced sounds becoming unvoiced. This put a high load on the remaining sounds /ʃ ṣ s/, and so most dialects of peninsular Spanish "solved" the similar-sound problem without any mergers by separating the three sounds, with /ʃ/ backing to /x/ and /s/ fronting to /θ/.
Take into account that in old Spanish, the letter X had the phoneme /x/ like the original title of the famous book Don Quixote, and the country name we still pronounce with /x/, México.
I always thought this had to do with greek letter chi (X), because of the obvious correspondence in grapheme and phoneme, but that would require somehow a direct connection from Greek to Spanish without passing through Latin, which I'm not sure of.
On the other hand, influence from Arabic does seem a plausible explanation.