In the past people measured the length of a year according to the moon's 28-day cycle but this obviously left a few days out, so I was wondering which civilization first realized this and fixed the problem?
there are 3 years, not two, that have to be coordinated. Stellar, Solar and Lunar.
The duration of none of these three can be expressed in whole days. (we have leap years because of this).
Every old calendar, including that of Mayans, Egyptians and Sumerians, worked. The only difference is how did they corrected for the difference of the solar, the lunar and the stellar years and fractional character of the years and months.
The Sumerian and Egyptian and old Roman calendars were practical ones - they simply added one or two days to a year when it was needed. Or subtracted.
The Julian calendar was theoretical one - it was the first known calendar that set the months and days in all years beforehand. So a common man could and can know beforehand when the spring comes.
But even Julian or later Gregorian calendars don't coordinate solar and lunar cycles. And their months has nothing in common with the real lunar months.
As for beforehand moon/sun coordination, it was Mayan calendar that counted for all and everything. They took into account even so named saros - superyear, the period of repetition of the Solar eclipses. Their calendar was very unpractical and complicated though. But precision was not bad - their predicted end of calendar had error only 3 days for 1000 years - very well for coordination of 4 independent cycles
Short and quick answer: definitely the Romans were NOT the first. Calendar of ancient Egyptians was solar, and Sumerian one - lunisolar, both of them fit your description, although the issue which of them is older is open, and in fact is a discussion of interpreting archeological materials. Hence it would be also impossible to provide any definitive, precise date for calendar development. Still, origin of both Egyptian and Sumerian one may be roughly dated to some time around 3000-2500 BC.
Maybe a word about the Greeks?
Around 330BCE, Callipus, a student of Eudoxus at Plato's Academy and of Aristotle at his Lyceum, determined the length of the tropical year (that is to say the interval of time between two vernal equinoxes or two summer solstices) to be 365+1/4 days. He also showed that the four seasons (defined as the interval of time between a solstice and an equinox) had different lengths (maybe the first realization of what we now understand as the eccentricity of the orbit of the Earth around the Sun).
Around 120BCE, Hipparchus, one of the most talented astronomer of the Antiquity, determined te length of the tropical year to be 365 days 5 hours and 55 minutes, which is within ten minutes of the exact value. He is considered to be the first to have noticed that this length of time is slightly shorter than the sidereal year (that is to say the length of time between the appearance of the Sun at two identical places with respect to the Zodiac, or in contemporary terms the time taken by the Earth to orbit the Sun once) which he might have estimated at 365 days 6 hours and 10 minutes (might because much of Hipparchus's is only known to us from subsequent quotations).
It seems that a lot of the phenomena described above (perhaps all save the discrepancy between tropical and sidereal year) was known to Chaldean (i.e late Babylonian) astronomers around 300BCE, but few primary sources on their knowledge survived.
Source: A History of Ancient Mathematical Astronomy O.Neugebauer.