The King's Leap. It occurred to early chess players that if in actual practice a mounted soldier could escape capture by leaping over his enemy, the King, who presumably would also be mounted, could escape in the same way. The King was surely as potent as one of his own Knights. And so there was introduced in Sanskrit chess the King's leap -- a privilege of moving like a Knight. This is still the rule in the Malay peninsula where once during the game (usually when "checkmated" -- or to escape checkmate) the King may leap like a Knight. [...] An initial two-move leap for the King was permitted in European chess down to the 13th century.
Castling. The root of castling is the King's leap [described above]. There were two forms of this leap. In the first, the King could jump once like a Knight. In the other, he had the privilege of going two squares on his first move. The latter form of the "King's leap" was the practice in European chess down to the 13th century.
In the North African variety of Arabic chess, the King was put into a safe corner by a two-stage procedure. First he moved into the second rank. Then the Rook was brought into the King's original square and simultaneously the King went to the Rook's corner. This was the embryo of modern castling and its development from there proceeded along logical lines. [...]
From North Africa [castling] crossed the Mediterranean along the trade routes and its first appearance in Europe was in Italy toward the close of the 15th century. For a while, Italy was the only country in Europe which used castling in chess play. As the Arabic crescent waxed around North Africa and crossed Gibraltar, the chess practices of the Moslems entered Spain and in the 16th century castling was the rule in Spanish chess. By this time, however, the Arabs had consolidated the maneuver into a single move, so that one-move castling was practiced on the Iberian peninsula and two-move castling on the Italian peninsula.
By 1630 castling was known in France, Britain, and Germany, though because of its dual ancestry, there was for a while some confusion as to the technic. [...] By the end of the 17th century, castling in the modern manner was a fixed rule of chess.
Davidson had a tendency to explain historical facts by speculative musings (e.g. 'It occurred to early chess players...' above). I've removed most of these from the quotes I selected.