Originally, a count or other noble would simply appoint a dependant as his representative to take charge of the castle. This wasn’t intended to be a hereditary position. When the old castellan died, the count could appoint a new one. The thing is, having charge of a castle gives one a certain amount of power and many began to take advantage of that. By the mid-11th century in France,* the position had become hereditary and castellan families had joined the ranks of the nobility. They still owed loyalty to their count,** but they were able to maintain a fair amount of independence.
It was in the 12th century though, that castellan families really began to rise to prominence. They began to take on the right of Banal Lordship. Where before they could only collect rents and had few political and no judicial powers, a Banal Lord had the right of command and could both extract taxes and judge disputes. They also began to hold a fair amount of power within the Church. By the end of the 12th century, members of castellan families dominated the Cathedral chapters in France and would soon come to do so elsewhere as well.
None of this is to say that a castellan couldn’t be removed from control of their castle*** or that one couldn’t appoint a new one, but by this point certain families had gained power and entered the nobility because they had charge of a castle, even if they didn’t own it themselves.
*It took a bit longer elsewhere.
**Or other lord.
***Though given that they had charge of a castle, it would be rather difficult to get them out if they decided to fight it.
Bouchard, Constance Brittain. Strong of Body, Brave & Noble: Chivalry & Society in Medieval France. Ithaca, NY: Cornell University Press, 1998.
Bouchard, Constance Brittain. Philadelphia, PA: University of Pennsylvania Press, 2001.
Gies, Frances and Joseph Gies. Marriage and Family in the Middle Ages. New York: Harper & Row, 1987.