In season 5 of Doctor Who, the Doctor is sealed in the ultimate prison (the Pandorica). It can only be opened from the outside (one wonders why the Pandorica would be designed to be opened after being sealed at all, but I digress). The Doctor manages to escape by travelling to the past and leaving clues for his companions so that they can let him out so that he can travel to the past and leave clues for his companions so that they can let him out so that he can travel to the past and you can probably get the point I am trying to make now. How did the Doctor manage to get out of the Pandorica to set this infinite loop into motion?
This is a case of the writer paradox.
The events can only unfold because the events have already happened.
In these episodes, the Doctor clearly insists on the fact that they experienced a paradox, but leaves it at that. And even though he followed the paradox, he never created it.
On a meta level Events that are shown to us are only the most entertaining timeline/alternate reality. So every timeline where it is interesting to see this paradox happen, we see it.
Digression : That's why we follow the Curtis family in the movie 2012, because they are the one who makes it. Similarly, we only see the reality where the Doctor makes it (most of the time).
The Pandorica is a special case though Indeed, every reality where this paradox didn't happen has been destroyed. So the paradox must happen. So there is no other reality to show us.
Technically, the reason the Doctor went back in time was because he had a mop and fez, and Rory pointed out that was how he looked in the past. This is what is referred to as a “temporal causality loop.” In other words, The Doctor getting freed from the Pandorica and him going back in time are both the cause AND effect of the other. It’s similar to the ending of the Doctor Who short “Time,” in which the TARDIS was inside itself but a little bit in the future, meaning if you entered it you would be transported slightly back in time. At the end, the Doctor needed to know which lever to push, and the future Doctor comes in and tells him, after which the present Doctor goes into the police box. Neither of these events could have happened without the other.