The Short Answer:
Some of it was here when the earth was made (but locked into the rocks as hydrates ... much of that released as the Earth became molten.)
And some came from comets and other ice particles that have bombarded the Earth since it was formed.
No one knows which part was the major contributor.
As the limestone dissolves, pores and cracks are enlarged and carry even more acidic water. Sinkholes are formed when the land surface above collapses or sinks into the cavities or when surface material is carried downward into the voids.