diff --git a/includes/theme.inc b/includes/theme.inc
index ed34b82..43bcd0e 100644
--- a/includes/theme.inc
+++ b/includes/theme.inc
@@ -1691,7 +1691,7 @@ function theme_status_messages($variables) {
       $output .= " </ul>\n";
     }
     else {
-      $output .= $messages[0];
+      $output .= reset($messages);
     }
     $output .= "</div>\n";
   }
diff --git a/modules/simpletest/tests/system_test.module b/modules/simpletest/tests/system_test.module
index 2eda351..ddb7b7b 100644
--- a/modules/simpletest/tests/system_test.module
+++ b/modules/simpletest/tests/system_test.module
@@ -78,6 +78,13 @@ function system_test_menu() {
     'type' => MENU_CALLBACK,
   );
 
+  $items['system-test/drupal-set-message'] = array(
+    'title' => 'Set messages with drupal_set_message()',
+    'page callback' => 'system_test_drupal_set_message',
+    'access callback' => TRUE,
+    'type' => MENU_CALLBACK,
+  );
+
   $items['system-test/main-content-handling'] = array(
     'title' => 'Test main content handling',
     'page callback' => 'system_test_main_content_fallback',
@@ -420,3 +427,17 @@ function system_test_authorize_init_page($page_title) {
   system_authorized_init('system_test_authorize_run', drupal_get_path('module', 'system_test') . '/system_test.module', array(), $page_title);
   drupal_goto($authorize_url);
 }
+
+/**
+ * Sets two messages and removes the first one before the messages are displayed.
+ */
+function system_test_drupal_set_message() {
+  // Set two messages.
+  drupal_set_message('First message (removed).');
+  drupal_set_message('Second message (not removed).');
+
+  // Remove the first.
+  unset($_SESSION['messages']['status'][0]);
+
+  return '';
+}
diff --git a/modules/system/system.test b/modules/system/system.test
index aefbfbc..673d51c 100644
--- a/modules/system/system.test
+++ b/modules/system/system.test
@@ -2825,3 +2825,32 @@ class SystemValidTokenTest extends DrupalUnitTestCase {
     return TRUE;
   }
 }
+
+/**
+ * Tests drupal_set_message() and related functions.
+ */
+class DrupalSetMessageTest extends DrupalWebTestCase {
+
+  public static function getInfo() {
+    return array(
+      'name' => 'Messages',
+      'description' => 'Tests that messages can be displayed using drupal_set_message().',
+      'group' => 'System',
+    );
+  }
+
+  function setUp() {
+    parent::setUp('system_test');
+  }
+
+  /**
+   * Tests setting messages and removing one before it is displayed.
+   */
+  function testSetRemoveMessages() {
+    // The page at system-test/drupal-set-message sets two messages and then
+    // removes the first before it is displayed.
+    $this->drupalGet('system-test/drupal-set-message');
+    $this->assertNoText('First message (removed).');
+    $this->assertText('Second message (not removed).');
+  }
+}
