While the solutions provided here do still work, WordPress has evolved since and does now (since 3.3, I believe) provide direct hooks for theme activation.
after_switch_theme
will fire on theme activation and switch_theme
before deactivating an old theme.
Hence the up-to-date answer is:
function reflush_rules() {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
add_action( 'after_switch_theme', 'reflush_rules' );