diff --git a/core/includes/form.inc b/core/includes/form.inc
index 85bffc6..b5ab69d 100644
--- a/core/includes/form.inc
+++ b/core/includes/form.inc
@@ -847,7 +847,10 @@ function drupal_process_form($form_id, &$form, &$form_state) {
     // cache when a form is processed, so scenarios that result in
     // the form being built behind the scenes and again for the
     // browser don't increment all the element IDs needlessly.
-    drupal_static_reset('drupal_html_id');
+    if (!form_get_errors()) {
+      // In case of errors no rebuild happens so elements still the same.
+      drupal_static_reset('drupal_html_id');
+    }
 
     if ($form_state['submitted'] && !form_get_errors() && !$form_state['rebuild']) {
       // Execute form submit handlers.
diff --git a/core/modules/simpletest/tests/ajax.test b/core/modules/simpletest/tests/ajax.test
index 8e731b8..72bf387 100644
--- a/core/modules/simpletest/tests/ajax.test
+++ b/core/modules/simpletest/tests/ajax.test
@@ -473,6 +473,12 @@ class AJAXMultiFormTestCase extends AJAXTestCase {
         $this->assertNoDuplicateIds(t('Updated page contains unique IDs'), 'Other');
       }
     }
+
+    // Tests that element ID's don't get duplicated when form validation fails.
+    $this->drupalGet('form-test/two-instances-of-same-form');
+    // Submit second node form with empty title.
+    $this->drupalPost(NULL, array(), t('Save'), array(), array(), 'page-node-form--2');
+    $this->assertNoDuplicateIds('There are no duplicate IDs');
   }
 }
 
