[... I have deliberately left a lot of the answer unreferenced, as it already ballooned, please ask in comments for any clarifications you desire for parts you consider unreferenced ...]
The reason there's a lot of confusion and conflict over government's legislation and
regulation of "marriage" is because "marriage" is 4 independent but interconnected things (there may be even more facets I didn't include, but 4 seems more than enough for our purposes):
A spiritual/romantic union of a set of people into a family. I'm loosely using the term "spiritual" here, since a vast majority of human culture in history viewed such union as being declared "in the eyes of $deity".
Please note that when viewed from this angle, the government really has no legitimate interest to legislate or regulate marriage. It's basically a Barney view of marriage: "I love you, you love me, we're a happy family".
A legal contract between a set of people (either explicit, or implicitly governed by prevailing laws). This is of course the main Wiki/dictionary definition.
On one hand, as a legal and financial matter, the government seemingly has some reasons to legislate here.
However, one can legitimately point out that there's no truly good reason for the government to get involved into the content of the contract (as opposed to its enforcement) between private parties.
Here, as in many other aspects, a more classical-liberal worldview lands on "no reason", simply because it is a private contract, whereas more pro-government minded people see just as much reason for government to legislate as in any OTHER private contracts (e.g. minimal wage laws, union laws).
Additionally, one can also raise an objection of "even if the state needs to legislate both the content AND the enforcement of the contrasts, why does the state then need to legislate WHO are the valid people to be able to enter into contract are?". Same story here - both liberals and conservatives consider it very much OK for the state to legislate who gets to enter into contracts (union laws and gay marriage, respectively); whereas libertarians consider it none of state's business ever.
An economic union of a set of people into a family to promote economic well-being.
Purely theoretically (I won't bother citing research unless asked in comments), people living a family life fare better economically than those living separately, which is beneficial to society at large. As such, the government may propose an argument that it has a legitimate reason to legislate marriage since it impacts on overall economic health of society. Personally, I find that argument weak but I feel compelled to state it as a legitimate one.
A cultural construct (an evolutionary stable strategy) which evolved in order to both regulate procreation, and to channel lower-status males into supporting and caring for offspring and society, driven by biological reality of being unable to verify male parentage before the advent of DNA testing in late 20th century (and humans being consummate K-strategists reproductively).
There are 3 reasons why governments legislate marriage:
A) First of all, if you ask "why civil government and not churches", the reason is historical. At some point in the past, churches decided to offload some of the bureaucratic functions on the civil government.
I think digging deeper into this belongs more on History.SE, but a fairly good summary can be found here. The main driver turning marriage into a state business in Europe was Protestantism (Martin Luther declared marriage to be "a worldly thing . . . that belongs to the realm of government") and for Catholic countries, French Revolution.
Please note that marriages were always "legislated" to a certain extent, by custom or religious laws. But the registration of marriage was much less formal - for example, in Christian Europe, before 1215, you didn't even need to be married in a church in Christian society, or by a priest. Ditto in Roman times.
Also note that in a lot of societies governed by religious governments (At the very least, all 3 major Abrahamic religions), at some point in history there was no difference - even when a separate civil government existed, all laws governing marriage were the religious laws, 100%. That was because in Abrahamic religions, a marriage was basically a religious social construct - commanded by and sworn in front of a deity.
Also, from historical point of view, the civil government taking over the legislation from churches was not due to "having to deal with many religions or atheists", as a naive guess may be made. The many relgions angle was never in play (E.g. Jewish religious authorities deal with marriage between Ashkenazi Jews in medieval Europe); and there weren't really any "atheists" as a demographics in Western countries until well after the state got into marriage game.
Also, from history, we see that all societies restricted who could get married (at the very least, most had incest prohibition).
B) Second, in the era of public benefits in 20th century, it's a way of distributing resources to dependents.
A government which distributes benefits for a family member (Social Security, public aid) needs to have a formal legal proof of dependency. This is somewhat of a facet of definitions #2 and #3 above.
C) Third, it's a way to control the society better, sometimes for "morality" purposes; to essentially prevent "bad for society" (from some governing group's point of view) marriages that are presumed to unbalance the equilibrum of society. From restricting interracial marriage in many states in USA in early 1900s, to restricting "immoral" polygamy of Mormons, to fights over gay marriage in late 1990s around the world.
Please don't mistake it for "right wing"/"conservative"/"theocratic" phenomenon - marriage was controlled a lot more rigidly in USSR and Nazi Germany than in the USA during the most rigid controls, without any religious component whatsoever.
This purpose basically stems from the view of marriage #4 in the list above.
As noted, this form of legislation/control was historically almost always present, but usually resided in church and not secular laws. The reasons and justifications vary, but the underlying motivations don't.
D. A way to control individuals in society. This is hard to clearly distinguish from (C), but basically this is more about controlling sexual urges/energy of everyone than about filtering out "bad" marriages. It was also clearly stemming from defition #4 above, but it was also more about pure sex than anything else (and as such, this form of control very frequently extended well beyond marriage, into controlling sexuality in general. See for example prohibition of porn in USSR).
As you can seen, when viewing the marriage from those distinct points of view, a society (as exemplified by government) may have some reasons, more or less legitimate, to legislate/regulate marriage, depending on which view you take.