diff --git a/core/includes/theme.inc b/core/includes/theme.inc
index 1768a76..b6bb339 100644
--- a/core/includes/theme.inc
+++ b/core/includes/theme.inc
@@ -2749,6 +2749,13 @@ function template_preprocess_page(&$variables) {
   if ($suggestions = theme_get_suggestions(arg(), 'page')) {
     $variables['theme_hook_suggestions'] = $suggestions;
   }
+
+  // Prepare render array for messages. drupal_get_messages() is called later,
+  // when this variable is rendered in a theme function or template file.
+  $variables['messages'] = array(
+    '#theme' => 'status_messages',
+    '#access' => $variables['show_messages'],
+  );
 }
 
 /**
@@ -2774,11 +2781,6 @@ function template_process_page(&$variables) {
     $variables['title'] = drupal_get_title();
   }
 
-  // Generate messages last in order to capture as many as possible for the
-  // current page.
-  if (!isset($variables['messages'])) {
-    $variables['messages'] = $variables['show_messages'] ? array('#theme' => 'status_messages') : '';
-  }
 }
 
 /**
