Is there a historical consensus on this? In Venice I remember reading that once the people elected their Doge after Roman rule collapsed, but how and when did they come to value democracy? And what about other Italian merchant republics?
OK, first of all, democracy =/= republicanism. I mention this for future reference. Medieval Italians, like the founding fathers of the United States would have feared democracy and its inherent volatility.
Second, while there is a "micro-period" in early medieval Italian history called, the feudalization of Italy, we should be careful when we employ the term. I mention this for future submissions because questions with the term "feudal" don't always get answers because it opens up a can of worms I'm not going to go into here, but there's a nice AMA from a while ago you may wish to peruse if you're interested in the sociopolitical order order of medieval Latin Europe.
The short answer is, Italy still had large scale landowners, and one of the major processes in the region during the 13th and 14th century was an attempt by the communes of Italy to expand their control into the contado and reign in their power. In 14th century Genoa, for example, when a popular government wrested power away from the aristocracies that dominated the political process, clans like the Fieschi withdrew to their massive landed holdings in Liguria and launched naval expeditions against the city.
The short answer to your question though is that in Italy, more so than any other region of the Latin speaking half of the Roman Empire, cities were a deeply ingrained part of life and society. Even as their infrastructures crumbled and the cities became more overgrown, people persisted in cities (they did shrink though). cities are much more likely to be sites of collective governance and rural regions with scattered settlements. Furthermore, landed nobles often maintained homes in these cities and would not have allowed any single one to dominate the scene politically.
Finally, I would like to note that the birth of the comune, which is what you are referring to in your question is a long and complex legal development that my brief answer doesn't begin to uncover. I have completely left out the role of cities as the seats of bishops, something that exerted a great influence on the emergence of civic institutions in medieval Italy.
Edit: Check out Waley and Dean's, The Italian City-Republics for an easy to digest description of this process.
In reality things aren't really all that clear cut. Remember, the Kings of the Polish-Lithuanian Commonwealth were elected officials. Seriously, all the various nobles got together when the old king kicked off to vote for a new king, usually a younger son of a foreign royal house. They picked up some really impressive ones, like Stephen Báthory from Hungary, and some real losers, like just about any Vasa you care to name that they picked up out of Sweden. Despite there being a strong legislative branch of the government and the top spot going to whomever got the most votes the franchise was limited to only members of the nobility.
In many feudal structures the count votes. It's an easy political expedient to represent those people whose views on stuff actually matters. This isn't "democracy" because the franchise was limited to only a handful of people who controlled major military or economic assets. The rule wasn't by "the people", and very often the demographic (ethnic, cultural, religious) make up of those who could vote and those who could not were very different.
Another major example of elections in the period was the selection of the Holy Roman Emperor by elector-counts. Remember, in the period there was no "Germany" there was, instead "The Germanies". There were more than a hundred German-Speaking, German-Cultured states scattered from modern France to modern Hungary. Most of them were members of the Holy Roman Empire and some of the most prominent and respected got to pick who would be Emperor next. This also wasn't democracy as the normal everyday German had no impact on the process at all, it was the rulers of Cologne, Bohemia, Saxony, and Trier who decided everything.
In these Italian States there was a roughly analogous process going on. In cities there were many distinct power blocks that ran their own militias, made their own money, and for all intents and purposes could rule their own neighborhood in their own rights. Many trade guilds operated a militia and collected impressive amounts of dues. Local churches had their own militias and collected tithes from its neighborhood. The large merchant families hired private security and ran businesses or banking operations that funded themselves independent of anything else. This contrasts a great deal with the condition of the French Countryside that people think of when they think "feudal" where the presence of a castle and its associated village largely monopolized power in the hands of the one economic and military unit. So, rather than a resident of the city engaging in warfare to put down the rival militias in a long running gangland-style feud it was easier to just give the Bishop or Guildmaster or pater familias of the merchant house a vote in the goings of the city and try to get the existing power blocs to pull in one direction.
It's also important to note that even in places like the Holy Roman Empire the cities developed their own independent political systems. While the powerful city-states of Italy never developed, many of the smaller cities of France, Germany, Poland, and the like banded together to form City-Leagues. The most famous of these is the Hanseatic League which had dozens of cities and operated as a government independent of other governments, sometimes fight and winning wars with various Kings.
Cities had power that sprang from a different source, and where cities were powerful they had a political system that reflected where their power came from. After the collapse of the Roman Empire many of the Roman-style cities of France, Germany, and England collapsed as the necessary support structured rotted away. In Italy they persisted, so while Italy did have counts and dukes and kings they also had the power that cities projected.
You might be interested in reading Hendrik Spruyt's The Sovereign State and It's Competitors which breaks down the internal power structure of City States, City Leagues, and a bit of the Feudal structure and explains why none of them persist to the modern day.