Strategy to avoid duplication when my paper is under-review
Sadly, priority is established by whoever is first to publish, not by when the work has been conducted (since this is normally not really traceable). However, depending on the discipline, there are normally multiple ways to handle this problem:


Posting a preprint: probably the easiest way to show that your work has existed before it appears in a peer-reviewed journal is to send a preprint to a preprint server, such as arXiv. How "useful" this is for establishing priority depends a bit on the discipline, but at least your work is out there and you have a fairly solid argument to convince people that you at least did not copy from whoever published before you (if it comes to that).
Multiple discoveries: in many disciplines, it's simply not true that once a paper on a specific idea has been published no further similar work can be accepted. Frankly, in an experimental discipline, as long as there are not a handful of similar experiments with results pointing in a similar direction a theory isn't really accepted anyway, so the second, third, or even tenth study may still provide sufficient value to be considered even at strong journals. I think in math things are different (but if you are in math you would probably post a preprint).
"Being vocal": it goes against the instinct of some people, but in my experience a good strategy to fight against losing priority is to tell as many people as you can what you are working on. If you already give talks at conferences about your preliminary results, tell your collaborators, publish partial results, or blog about it, the word what you are working on is out there long before your paper actually appears. This has two effects. (1) It detracts people from working on your topic in parallel (or, if they already are, seek collaboration). Your competitors fear the same thing you fear (and, if they are just getting started, they may feel that you are way ahead of them). (2) Even if somebody beats you to publication, the community is well aware that you and your competitor just worked in parallel on the same topic (see also "multiple discoveries").