In 1821 , Costa Rica became one of several Central American provinces that jointly declared their independence from Spain .