How Parent Taxonomy automatically added to the post when using `wp_set_object_terms()`?

You can use get_ancestors() to get parents and grandparents etc. of a term, then use that in wp_set_object_terms():

$term_id = 12;
// Get array of term parents.
$terms = get_ancestors( $term_id, 'topic' ); 
// Include original term in array.
$terms[] = $term_id; 
// Add parents and original term to post.
wp_set_object_terms( $post_id, $terms, 'topic', true );