A few days ago I caught a train heading North from Verona through Northern Italy towards Bavaria and noticed quite a few castles in the hills/valleys near towns. My understanding of Italy in the middle ages is that it was primarily made up of independent or loosely affiliated city states. What was going on in this region just South of Bavaria at this time? Where did these castles come from and who did they belong to?
Of course, city states would never ever create a social situation whereby castles would become a thing. No no, independent polities competing with each other in a narrow peninsula would clearly always get along.
Or not.
I'm tempted to start my post with why there were so many city-states in Italy, I've already gone on digressions to that effect in the past and would probably bore you to death. I'm still deriving this answer from the one posted above.
So let's jump right to the eleventh century. The old roman cities (and some new ones) are the points of reference for local life; rendered all the more important by being seats of Bishops, who often acted as points of authority for local lords. The wealth of these cities meant public life was never really centered around the household of a single feudal landholder as in the rest of Europe. In Milan, for example, the landed families of Della Torre, Argentea, Baggio, Busto, Grassi and Caronia, along with many other smaller ones, all converged onto the Bishop of Milan instead of a classically "Feudal" overlord.
So let's clarify this point: Feudal Landholders existed in Italy. But thanks to the density of cities, the convergence of so many (locally) wealthy and powerful individuals, along with the influence of an "Artisan" class that could only exist thanks to the density of urbanization, meant that many cities developed a unique, vaguely democratic system of government. Although the system changed from city to city, most established a legislative/electoral council consisting of representatives from leading families, and often, the leaders of local artisans of merchants guilds were allowed to participate as well. Many cities elected a Podestà , who was often a leading citizen of another city to act as an arbiter. Thus, by the Year 1002, the cities of Crema, Cremona, Milan, Monza, Bergamo and Brescia were all thriving "Communal Republics".
Along with the Podestà , the most important elected job in the communes was that of Capitano del Popolo, or Captain of the People. He acted as a Commander-in-Chief when the city was threatened with war by a rival city, and commanded the cities' forces should they decide to conquer a neighboring city. Milan, Verona, Florence, and to a lesser extent, Padua, gained a reputation as "Bully" cities in northern Italy, expanding their territory far beyond their outskirts.
So you can imagine, then, why a prominent landowner near, say, Verona, would still have an incentive to fortify his county seat. Of course the city was the center of life (and where he would spend large chunks of his time), but his revenue, the rents he collected, and the income he derived from trade was still very much tied to his estates. Should the Milanese or Venetians take up arms against his city, he still had every interest (and obligation) to protect his serfs, especially if he and/or his sons were off to fight for his city.
That's not to mention the infighting that could happen inside a single city. Direct democracy, even if limited to a few dozen individuals, can mean things get rather messy. Starting from the mid 13th all the way up to the early 15th century, there was a trend for one family to dominate the others, and either pay lip service to the communal institutions while really ruling alone (like the Carrara in Padua, the Dovara in Cremona, and famously, the Medici in Florence) or sometimes outright dismantle them, often via bloody internal struggle against opposing families (like the Visconti in Milan, the Scaligeri in Verona, and the Ezzellini in Treviso and Vicenza). Keep in mind, it's not like these families had dropped everything to move to the cities; they were still feudal landholders, who held large estates and castles outside the cities from which they could call their own vassals to arms; many powerful lords, such as the Visconti's rivals, the Della Torre, were feudal lords of entire fortified burghs in addition to their various estates. Although they might draw as much income from trade as from rents and agriculture, the two things are linked; you need capital to start trade ventures, and society was still mostly agricultural.
Lastly, your train to Germany is travelling up the valley of the Adige River. Historically, that valley was ruled by the Prince-Bishop of Trent. Close to the border with the Holy Roman Empire, historically very close to the crown of Austria, and historically less dense and wealthy than the Padanian Plain, the feudal hierarchy was much stronger there (and much more dependent on land held by ecclesiastical institutions, a further testament to its slower development). Further, the Adige Valley leads to the Brenner Pass, which to this day remains an important crossing-point over the Alps and into Italy. With the occasional Imperial Army coming through, along with all sorts of ruffians and bandits that prayed on travelers, the construction of castles was more than accelerated. Also, keep in mind that although cities like Milan, Verona, and even Padua could once count on a ring of castles owned by the city's prominent families to mark out their territory and provide a first line of defense against invaders, the advent of gunpowder meant that many of these were converted to country villas, while others were outright demolished to make room for industrial/suburban sprawl on the last sixty years (or earlier Peidmont and Western Lombardy, the epicenter of the Italian Industrial Revolution). The growth of the Trentino-Alto Adige Region you saw on your train ride, on the other hand, is only really attributable to events of the past thirty-forty years (today, it is one of Italy's wealthiest regions, but this was not the case in the past). Consequentially, many medieval castles remained untouched.