This might have been related to the earliest forms of organized religion, but that explanation still seems a bit simplistic to me. In any case, much of this wasn't the case for Christianity (or Islam, or later religions I can think of). Christianity became popular in the ancient Mediterranean world, which had a long recorded history, including history of laws, and the beginnings of the modern legal system, particularly in the Roman Empire (to be clear, I know less about other "legal systems" of the ancient world, but the case could probably be made that, say, Egypt or Greece had judicial systems which more or less attempted to do what you suggested). What certainly isn't true is that anyone converted to Christianity because it provided a proto-scientific explanation for why the sun rose. Beyond the larger sense that "the sun rises because God ordained it so", there's no "early science" at all in Christianity. Even the book of Genesis wasn't believed by Jews 2,000 years ago to be a factual account of the world, and Christians in general didn't tend to believe it either; if you were to look at early Christian commentaries on Genesis, I don't think you'd find a single one which would actually express a belief that it was factual.However, to give my opinion, the church originally served as a repository for tribal laws and told stories about why those laws were important to the tribe. That function is now held by the government. The advantage that the church had was that it had a longer time horizon than the latest king and so could endorse rules which had a proven history behind them.
The church also provided a rudimentary explanation for why the sun rose and the rains came. It was an early form of science, although empiricism was under-appreciated for a very long time.
It also served as a meeting place for people to get together on safe ground to listen to each other and to work out their differences in the presence of an impartial judge who followed civilized rules of engagement. That function is now held by governments and the courts and by your personal psychiatrist.
Some Christians today insist that the events of Genesis literally happened because on some level they've come to accept empiricism as the only valid means of understanding the world, and feel they have to defend the Bible in that context. But this is an entirely modern phenomenon. This is painting with a large brush, but, say, a Greek in the second century might not believe in the classical gods, but he wouldn't consider the story of Deucalion and Pyrrha making people out of stones, because it seemed implausible, "bullshit" (as a modern atheist might put it), or irrelevant to his life, because it was a part of his cultural context and history. Or a Greek might believe that Zeus existed, but that particular story might stretch his belief; or so on, but would still enjoy listening to a telling of that myth, completely independently of believing it to provide a believable explanation of the world.
Sure, that's a pretty important function of a church. But that's not why people begin to attend one, and the proof is that, as you've said, there is no adequate substitute for this function, yet churches are dwindling.Finally the church was often a source of entertainment, and that function is now performed by the entertainment industry and by the internet.
What functions does the church do better than anyone else? My personal opinion is that it brings people together in real life in an arena which is safe and accepting to all, and it promotes respectful discourse, both of which are under-valued by the average person but are slowly being better appreciated as people age. It is really impossible at this point to name an adequate substitute for face-to-face interactions.
@Lord Pixel , @ooo