To my knowledge every Starfleet ship we see is commanded by a Captain. From what I know of modern navies: Lieutenants, Lieutenant Commanders, and Commanders can also command ships. I'm just curious as to why, in starfleet, we never see this.
The Memory Alpha article on Starfleet Captains makes several references to Junior Captains, those who've commanded a Starfleet vessel but don't yet hold the rank of 'Captain'.
- Commander Riker, William T. (USS Hathaway, USS Excalibur, and USS Enterprise-D)
- Lieutenant Commander Data (USS Sutherland and USS Enterprise-D)
- Lieutenant Commander Jadzia Dax (USS Defiant)
- Lieutenant Commander Piersall (USS Prometheus)
- Lieutenant Commander James T. Kirk (USS Enterprise)
- Cadet Tim Watters (USS Valiant)
In short, you don't have to be a captain to captain a vessel, but when you do, you're referred to as "captain" regardless of rank. On top of that, most captains are ranked as captains as a recognition of their status within Starfleet and in general recognition that they're competent to lead a large team, head up an important project or be in charge of a vessel.
On a similar note, there are also Senior Captains, those of flag rank who choose to take over a single vessel. Again, these individuals are referred to as "captain" for the duration of their command.
The ranks in Star Trek are taken from the Navy ranks. In the Navy a Captain is not only a rank but also a title. Someone does not have to be a Captain to command a naval ship. But any officer commanding a naval ship is considered the Captain of that ship regardless of their rank. That's why a Commander can be considered a Captain if they command a ship. While on that ship you would be accurate to refer to that commanding officer as "Captain" when addressing them. But on land you would refer to them by their rank.
The other answers address officers below the rank of Captain commanding ships. We can also go the other way, though: James T. Kirk was an Admiral when he commanded the Enterprise in Wrath of Khan.
I vaguely remember an episode of TNG where a Starfleet ship was hailed and that person addressed himself as the captain of that vessel, however, the rank on his collar was that of a commander. I'm not quiet sure which episode it was but I'm 75% positive I saw it. Now, whether or not he was the highest senior officer or just filling in for him, I don't know, but all I remember is that he addressed himself as the captain and no person of captain rank was mentioned. From what I remember the part was really small, a single 20-second conversation between the Enterprise and some random other vessel.
Despite there being no proof to actually back this up, the scene stuck in my mind because it made me think of the USN where people who hold the rank of commander are captains of some vessels. However, maybe this is proof that this tradition does exist in star trek. Also if you recall in DS9, Sisqo held the rank of Commander for a while. It would be interesting if Star Trek ever actually adopted this tradition because it would open up a whole new type of command structure concerning vessels deemed less important by the federation.