diff --git a/hgext/shelve.py b/hgext/shelve.py
index de654a83fe1c95949effe7e6e7598d60364d8b3a_aGdleHQvc2hlbHZlLnB5..4f8c20fe66f03f3e69fea62f364ebff33c9aa51c_aGdleHQvc2hlbHZlLnB5 100644
--- a/hgext/shelve.py
+++ b/hgext/shelve.py
@@ -163,4 +163,8 @@
     maxbackups = repo.ui.configint('shelve', 'maxbackups', 10)
     hgfiles = [f for f in vfs.listdir() if f.endswith('.hg')]
     hgfiles = sorted([(vfs.stat(f).st_mtime, f) for f in hgfiles])
+    if 0 < maxbackups and maxbackups < len(hgfiles):
+        bordermtime = hgfiles[-maxbackups][0]
+    else:
+        bordermtime = None
     for mtime, f in hgfiles[:len(hgfiles) - maxbackups]:
@@ -166,4 +170,7 @@
     for mtime, f in hgfiles[:len(hgfiles) - maxbackups]:
+        if mtime == bordermtime:
+            # keep it, because timestamp can't decide exact order of backups
+            continue
         base = f[:-3]
         for ext in 'hg patch'.split():
             try:
@@ -558,6 +565,12 @@
     backup directory. Only the N most recent backups are kept. N
     defaults to 10 but can be overridden using the shelve.maxbackups
     configuration option.
+
+    .. container:: verbose
+
+       Timestamp in seconds is used to decide order of backups. More
+       than ``maxbackups`` backups are kept, if same timestamp
+       prevents from deciding exact order of them, for safety.
     """
     abortf = opts['abort']
     continuef = opts['continue']
diff --git a/tests/test-shelve.t b/tests/test-shelve.t
index de654a83fe1c95949effe7e6e7598d60364d8b3a_dGVzdHMvdGVzdC1zaGVsdmUudA==..4f8c20fe66f03f3e69fea62f364ebff33c9aa51c_dGVzdHMvdGVzdC1zaGVsdmUudA== 100644
--- a/tests/test-shelve.t
+++ b/tests/test-shelve.t
@@ -185,6 +185,16 @@
 
 apply it and make sure our state is as expected
 
+(this also tests that same timestamp prevents backups from being
+removed, even though there are more than 'maxbackups' backups)
+
+  $ f -t .hg/shelve-backup/default.hg
+  .hg/shelve-backup/default.hg: file
+  $ touch -t 200001010000 .hg/shelve-backup/default.hg
+  $ f -t .hg/shelve-backup/default-1.hg
+  .hg/shelve-backup/default-1.hg: file
+  $ touch -t 200001010000 .hg/shelve-backup/default-1.hg
+
   $ hg unshelve
   unshelving change 'default-01'
   $ hg status -C
@@ -196,6 +206,17 @@
   R b/b
   $ hg shelve -l
 
+(both of default.hg and default-1.hg should be still kept, because it
+is difficult to decide actual order of them from same timestamp)
+
+  $ ls .hg/shelve-backup/
+  default-01.hg
+  default-01.patch
+  default-1.hg
+  default-1.patch
+  default.hg
+  default.patch
+
   $ hg unshelve
   abort: no shelved changes to apply!
   [255]