there are a few theories. naturally there's the spiritual explanation and then science.
spiritually, we've all lived many lives, and in living those lives, likely been to the same places we may visit now. the lives we lived previously shape our current personality and make us who we are. so we carry our past experiences with us, and when we see something familiar, we recognize it, even if we haven't been there in this lifetime.
science says when you're mentally fatigued your brain gets confused and triggers a deja vu like experience where you THINK you've done something before, but the relay between whats happening at this very second and what happened 2 seconds ago is delayed from the fatigue that it feels like it happened at another point in our lives. essentially your brain is processing the event at a much lower rate of speed than you're experience, giving you the feeling you've done it before when really it just happened.