If I accidentally write something that someone else has already written, is that plagiarism?

Technically, yes but it happens. Even if you did not realize your plagiarizing you can still have some consequences.