This frequently happens to a lot of people. Think of it like this:
You build a nether portal, you enter it
Nether portal spawns in nearest cave/opening
However, when the coordinates are calculated from nether portal back to overworld, it cannot find your orginal portal, and therefore makes a new one where the portal is supposed to be (based on portal nether coordinates).
Try building your overworld portal closer to where the nether portal kept spawning in. It should link up.
This answer does not contain how nether portals really work so as to keep it simple.
This issue has been addressed here: